[ccJULY14] Reach The Point

描述 Description

Recently Chef bought a bunch of robot-waiters. And now he needs to know how much to pay for the electricity that robots use for their work. All waiters serve food from the kitchen (which is in the point (0, 0)) and carry it to some table (which is in some point (x, y)) in a shortest way. But this is a beta version of robots and they can only do the next moves: turn right and make a step forward or turn left and make a step forward. Initially they look in direction of X-axis. Your task is to calculate for each query the number of moves they’ll do to reach corresponding table.

输入格式 InputFormat

The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. For each test case there is a sing line containing two space-separated integers - x and y.

输出格式 OutputFormat

For each test case, output a single line containing number of moves that robot will make to reach point (x, y)

样例输入 SampleInput

2
3 3
3 4

样例输出 SampleOutput

6
7


CodeChef RETPO


代码 Code

如图所示找规律即可。

#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int i,j,t,n,m,l,r,k,z,y,x;
int T;
int ans;
int main()
{
    scanf("%d",&T);
    while (T--)
    {
        scanf("%d%d",&x,&y);
        x=abs(x);y=abs(y);
        ans=0;
        if (x<y)
        {
            t=y-x;
            ans+=4*(int)(t/2);
            t%=2;
            if (t) ans++;
            ans+=x*2;
        }
        else
        {
            t=x-y;
            ans+=4*(int)(t/2);
            t%=2;
            if (t) ans+=3;
            ans+=y*2;
        }
        printf("%d\n",ans);
    }
    return 0;
}