Description:
题目大意:有一个无限的二维平面。一个 robot 在(0,0)的位置,可以上下左右移动,也可以选择呆在原地不动,给出一个(x,y)求出到该位置的最小步数,要求每一步不能和上一步动作相同。
解题思路:
算法标签:Math
一看题目,二话不说想BFS一下。仔细分析发现 x>0,y>0,所以 robot 只选择向上向右走,或者呆在原地不动。如果 x = y时,显然要走 x+y 步(向上向右交替走就好了)。但是 x
≠
\neq
= y 时,结果显然不正确。取 minn = min(x,y),发现可以走到(minn,minn)后,与目标位置在一条线上,将二维简化为一维,如果当距离小于 1 时,显然再走一步即可(因为到达位置(minn,minn)你的上一步选择向右或者向上均可,所以可以直接走就行)。当距离大于 1 时,显然只需要走一步停一步是步数最小的方式,一直到距离为 1 的时候,然后再走一步即可。
代码:
// TSWorld
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <map>
#include <stack>
#include <cmath>
#include <queue>
using namespace std;
const int N = 10005;
int main()
{
int T = 0;
int x = 0,y = 0;
int ans = 0;
cin>>T;
while(T--) {
scanf("%d%d",&x,&y);
if(x == y)
cout<<x+y<<endl;
else {
int minn = min(x,y);
int maxx = max(x,y);
ans = 2*minn;
if(maxx - minn <= 1) {
ans += maxx - minn;
}
else {
ans += (maxx - minn - 1)*2+1;
}
cout<<ans<<endl;
}
}
}