HDU 2717
来源:互联网 发布:立体设计图软件 编辑:程序博客网 时间:2024/05/29 14:00
Catch That Cow
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8764 Accepted Submission(s): 2762
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?
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
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8764 Accepted Submission(s): 2762
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?
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
//此题是在一条水平线上追赶羊,分为三种情况 x-1 /x+1 /2*x 注意边界即可
#include <stdio.h>#include <string.h>#include <queue>using namespace std;int n,start,stop;int p[210];int vis[210];struct Node{ int floor; int step;};int bfs(int flo){ queue <Node> q; memset(vis,0,sizeof(vis)); Node a; a.floor=flo,a.step=0; q.push(a); while(!q.empty()) { Node b=q.front(); q.pop(); vis[b.floor]=1; if(b.floor==stop) return b.step; for(int i=0;i<2;i++) //0 up 1 down { Node c=b; if(i==0) c.floor=c.floor+p[c.floor]; else c.floor=c.floor-p[c.floor]; if(!vis[c.floor]&&c.floor>=1&&c.floor<=n) { c.step++; q.push(c); vis[c.floor]=1; } } } return -1;}int main(){ while(~scanf("%d",&n)&&n) { scanf("%d%d",&start,&stop); for(int i=1;i<=n;i++) scanf("%d",&p[i]); printf("%d\n",bfs(start)); } return 0;}
0 0
- HDU 2717
- hdu-2717
- HDU 2717
- HDU-2717
- Hdu 2717
- HDU 2717
- HDU-2717
- [HDU-2717] BFS
- hdu 2717 bfs()
- hdu~2717(bfs)
- hdu 2717 bfs
- hdu 2717 解题报告
- HDU 2717(STL)
- hdu 2717 BFS
- 图论 BFS HDU 2717
- hdu 2717 bfs
- HDU/HDOJ 2717
- hdu 2717 (一维广搜)
- 传智播客全新升级入主武汉金融港。
- MathType中关于箭头符号的那些秘密
- 一道zhangying机试题
- C++命名空间
- JSon对象、JSon数组和JSon字符串。
- HDU 2717
- 动态设置UITableViewCell的高度
- 表达式解析器 IKExpression
- hdu 1004 Let the Balloon Rise(trie||映射统计)
- 【Python 学习手册笔记】动态类型简介
- 内存映射数据交换
- 黑马day17 json格式的数据
- Xutils 总结
- Android 屏幕旋转 处理 AsyncTask 和 ProgressDialog 的最佳方案