POJ-3278-Catch That Cow
来源:互联网 发布:二维码生成算法 c语言 编辑:程序博客网 时间:2024/06/15 18:09
Description
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting.
* Walking: FJ can move from any point X to the points X - 1 or X + 1 in a single minute
* Teleporting: FJ can move from any point X to the point 2 × X in a single minute.
If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?
Input
Line 1: Two space-separated integers: N and K
Output
Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.
Sample Input
5 17
Sample Output
4
Hint
The fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes.
这是一道简单的BFS题,只是将以前的4个方向变成两个方向就行。同时这里需要注意开vis数组的时候一定要开大些,不然会一直出现RE
代码如下:
#include <cstdio>#include <queue>#include <algorithm>#include <cstring>typedef long long ll;using namespace std;ll maxn = 200005;ll n,k;ll vis[200005];struct node{ ll x,time;};bool check(ll x){ if(vis[x]) return true; else if(x<0) return true; return false;}ll bfs(){ node s,next; s.x = n; s.time = 0; vis[s.x]=1; queue<node>q; q.push(s); while(!q.empty()) { s = q.front(); q.pop(); if(s.x==k) return s.time; for(int a = 0; a < 3; a ++) { next = s; if(a==0) next.x = s.x+1; else if(a==1) next.x = s.x-1; else if(a==2&&s.x<k) next.x = s.x*2; if(check(next.x)) continue; next.time = s.time+1; vis[next.x]=1; q.push(next); } } return 0;}int main(){ scanf("%lld%lld",&n,&k); memset(vis,0,sizeof(vis)); ll ans = bfs(); printf("%lld",ans); return 0;}
阅读全文
0 0
- poj 3278 Catch That Cow
- POJ 3278 Catch That Cow
- poj 3278 catch that cow
- poj 3278 Catch That Cow
- POJ 3278 Catch That Cow
- Poj 3278 Catch That Cow
- POJ 3278 Catch That Cow
- POJ 3278 Catch That Cow
- POJ 3278 Catch That Cow
- poj 3278 Catch That Cow
- POJ 3278 Catch That Cow
- poj 3278 Catch That Cow
- poj 3278 Catch That Cow
- POJ 3278 Catch That Cow
- POJ 3278 - Catch That Cow
- POJ 3278 Catch That Cow
- POJ 3278 Catch That Cow
- POJ-3278-Catch That Cow
- 关于反向传播算法中几个公式的推导
- 51之定时器
- LeetCode-54-Spiral Matrix 模拟
- Day2
- hdu6208(cin取消同步)
- POJ-3278-Catch That Cow
- Mysql优化
- 在ArcGIS中如何设置选择要素的颜色
- Websockets和SSE(服务器发送事件的疑问
- mysql 复习
- As修改项目名,模块名,包名
- Serlvet3.0 注解 浅析
- hdu 1262 寻找素数
- [effectiv c++]条款33:避免遮掩继承而来的名称(重载,重写,重定义)