Codeforces Raif Round 1 (Div. 1 + Div. 2) A. Box is Pull

A. Box is Pull
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.
For example, if the box is at the point (1,2) and Wabbit is standing at the point (2,2), he can pull the box right by 1 unit, with the box ending up at the point (2,2) and Wabbit ending at the point (3,2).

Also, Wabbit can move 1 unit to the right, left, up, or down without pulling the box. In this case, it is not necessary for him to be in exactly 1 unit away from the box. If he wants to pull the box again, he must return to a point next to the box. Also, Wabbit can’t move to the point where the box is located.

Wabbit can start at any point. It takes 1 second to travel 1 unit right, left, up, or down, regardless of whether he pulls the box while moving.

Determine the minimum amount of time he needs to move the box from (x1,y1) to (x2,y2). Note that the point where Wabbit ends up at does not matter.

Input
Each test contains multiple test cases. The first line contains a single integer t (1≤t≤1000): the number of test cases. The description of the test cases follows.

Each of the next t lines contains four space-separated integers x1,y1,x2,y2 (1≤x1,y1,x2,y2≤109), describing the next test case.

Output
For each test case, print a single integer: the minimum time in seconds Wabbit needs to bring the box from (x1,y1) to (x2,y2).
Example
input
2
1 2 2 2
1 1 2 2
output
1
4
My Answer Code:

/*
	Author:Albert Tesla Wizard
	Time:2020/10/28 13:53
*/
#include<bits/stdc++.h>
using namespace std;
using ull=unsigned long long;
int main()
{
    
    
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    vector<vector<int>>a(t,vector<int>(4));
    vector<int>ans(t);
    for(int i=0;i<t;i++)
    {
    
    
        cin>>a[i][0]>>a[i][1]>>a[i][2]>>a[i][3];
        if((a[i][0]==a[i][2])||(a[i][1]==a[i][3]))
        {
    
    
            ans[i]=abs(a[i][0]-a[i][2])+abs(a[i][1]-a[i][3]);
        }
        else
        {
    
    
            ans[i]=abs(a[i][0]-a[i][2])+abs(a[i][1]-a[i][3])+2;
        }
    }
    for(int i=0;i<t;i++)cout<<ans[i]<<'\n';
    return 0;
}

猜你喜欢

转载自blog.csdn.net/AlberTesla/article/details/109331147