Codeforces 1452A Robot Program(Math)

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;
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值