hdu 2717 Catch That Cow
来源:互联网 发布:镜仙网络电影 编辑:程序博客网 时间:2024/05/22 03:01
点击打开链接
Catch That Cow
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 16120 Accepted Submission(s): 4823
Problem 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?
* 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
4HintThe 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有思路的题,也是简单题,思路也没问题,但是一直是Runtime Erro,真搞不懂,后来看了看别人的,还是细节。细节,细节。
#include<cstdio>#include<queue>#include<cstring>using namespace std;const int N = 1000000;int vis[N+10];int n,k;struct node{ int x,step;};int check(int x){ if(x<0||x>=N||vis[x]) return 0; return 1;}int bfs(int x){ queue<node>Q; node a,next; a.x=x; a.step=0; vis[x]=1; Q.push(a); while(!Q.empty()) { a=Q.front(); Q.pop(); if(a.x==k) return a.step; next.x=a.x+1; if(check(next.x)) { next.step=a.step+1; vis[next.x]=1; Q.push(next); } next.x=a.x-1; if(check(next.x)) { next.step=a.step+1; vis[next.x]=1; Q.push(next); } next.x=a.x*2; if(check(next.x)) { next.step=a.step+1; vis[next.x]=1; Q.push(next); } } return -1;}int main(){ while(~scanf("%d%d",&n,&k)) { memset(vis,0,sizeof(vis)); int ans=bfs(n); printf("%d\n",ans); } return 0;}
阅读全文
0 0
- hdu 2717 Catch That Cow
- HDU-2717-Catch That Cow
- HDU 2717 Catch That Cow
- hdu 2717Catch That Cow
- hdu 2717 Catch That Cow
- HDU-2717-Catch That Cow
- hdu 2717 Catch That Cow
- hdu 2717 Catch That Cow
- HDU 2717 Catch That Cow
- hdu 2717 Catch That Cow
- hdu 2717 Catch That Cow
- HDU 2717 Catch That Cow
- HDU 2717 Catch That Cow
- hdu 2717 Catch That Cow
- HDU 2717Catch That Cow
- HDU 2717 Catch That Cow
- hdu 2717 Catch That Cow
- hdu 2717 Catch That Cow
- C++派生类的构造函数
- 编码
- BUAA OJ 166 回文数
- SSM执行流程
- 5-25 朋友圈 (并查集)
- hdu 2717 Catch That Cow
- JVM 垃圾回收器工作原理及使用实例介绍
- 构建vue全家桶项目-引入vue-resource
- bzoj1036 树的统计Count
- JAVA WEB中获取路径的方式
- 【最大流-Dinic算法】B
- 第2章 Spring框架介绍 -- Spring4.3.8参考文档中文版
- 第三篇:二十年亲历企业信息化(1)
- 文章标题