A. Box is Pull

蔺德曜
2023-12-01

题目:
Wabbit is trying to move a box containing food for the rest of the zoo in the coordinate plane from the point (x1,y1) to the point (x2,y2).

He has a rope, which he can use to pull the box. He can only pull the box if he stands exactly 1 unit away from the box in the direction of one of two coordinate axes. He will pull the box to where he is standing before moving out of the way in the same direction by 1 unit.

https://codeforces.com/contest/1428/problem/A

题意:
类似于推箱子的拉箱子,每次只能拉动一个格子,只能拉直线,拉一格需要一秒。那对于给出的任意两个点,只要他们在一条直线上,时间就是这两点的距离。如果不在一条直线上,那最短的路线就是两条直线,先将箱子从起点拉到与重终点在一条直线上,然后拉箱子的人改变方向(改变方向需要走两个格子),然后拉到终点。答案也就是两点间的距离加二。

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
long long int res = 0;
int main()
{
	int n;
	cin >> n;
	while(n--)
	{
		int a,b,c,d;
		cin >> a >> b >> c >> d;
		if(a==c)
		{
			res = abs(d-b);
			cout << res << endl;
			continue;
		}
		if(b==d)
		{
			res = abs(a-c);
			cout << res << endl;
			continue;
		}
		res = abs(a-c)+abs(b-d)+2;
		cout << res << endl;
	}
	return 0;
}
 类似资料:

相关阅读

相关文章

相关问答