Catch That Cow
2015-08-05 21:16
330 查看
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9254 Accepted Submission(s): 2906[align=left]Problem Description[/align]
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?
[align=left]Input[/align]
Line 1: Two space-separated integers: N and K
[align=left]Output[/align]
Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.
[align=left]Sample Input[/align]
5 17
[align=left]Sample Output[/align]
4 HintThe 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.
题目要求是让你找直线上从a点到b点的最短距离,没有那么简单,是有条件的,要满足这两个条件:
* 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.//距离是上一段距离的2倍;
程序写好后,提交时发现编译错误,经过查找,发现结构体变量stu now,next;从全局变量变成函数bfs里的变量后在杭电上提交正确
#include<stdio.h> #include<cstring> #include<queue> using namespace std; int n,k; int a[200005]; struct stu{ int on; int step; }; //struct node //{ // int on, step; // friend bool operator < (node a, node b) // { // return a.on > b.on; // } //}; //priority_queue<node>q; queue<stu>q; //priority_queue<stu>q; //int M=100004; //int IN=1000000; int bfs() { stu now,next; while(!q.empty()) q.pop(); now.on=n; now.step=0; a[now.on]=1; q.push(now); while(!q.empty()) { now=q.front(); q.pop(); // next.on=now.on; // next.step=now.step; for(int i=0;i<3;i++) { if(i==0) next.on=now.on+1; if(i==1) next.on=now.on-1; if(i==2) next.on=now.on*2; next.step=now.step+1; if(next.on==k) return next.step; if(next.on<0||next.on>100000) continue; if(!a[next.on]) { a[next.on]=1; q.push(next); } } } } int main() { int h; while(scanf("%d%d",&n,&k)!=EOF) { memset(a,0,sizeof(a)); if(n<k) { h=bfs(); printf("%d\n",h); } if(n==k) printf("0\n"); if(n>k) printf("%d\n",n-k); } return 0; }
再贴一个简单代码:
#include<stdio.h> #include<cstring> #include<queue> using namespace std; int n,k; int a[200005]; struct stu{ int on; int step; }; int bfs(int n,int k) { queue<stu>q; stu now,next; now.on=n; now.step=0; a[now.on]=1; q.push(now); while(!q.empty()) { now=q.front(); q.pop(); if(now.on==k) return now.step; for(int i=0;i<3;i++) { if(i==0) next.on=now.on+1; if(i==1) next.on=now.on-1; if(i==2) next.on=now.on*2; if(a[next.on]||next.on<0||next.on>100000) continue; a[next.on]=1; next.step=now.step+1; q.push(next); } } } int main() { int h; while(scanf("%d%d",&n,&k)!=EOF) { memset(a,0,sizeof(a)); h=bfs(n,k); printf("%d\n",h); } return 0; }
相关文章推荐
- sourceIsight创建大量工程时的注意事项
- UE4蓝图术语表
- poj1631 dp 最长上升子序列LIS
- 网站大背景(按比例缩放背景图片)
- 模板——矩阵快速幂
- iOS- 如何将应用集成发短信、发邮件、打电话
- tomcat进程意外退出的问题分析
- c++封装线程池
- ios 图片缩放失真 (1)
- mac中安装activeMQ
- LeetCode(13) Roman to Integer
- Mac 安装 MySQL
- Minimal coverage(贪心,区间覆盖)
- LeetCode(13) Roman to Integer
- 【方言】Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
- UVALive 4128 Steam Roller 蒸汽式压路机(最短路,变形) WA中。。。。。
- 暑假集训第四周周三赛 D - Pass-Muraille 穿墙 贪心
- TCP模型及其重点协议总结
- Quartz2D - 04.利用贝瑟尔路径(UIBezierPath)绘制基本图形
- 方法