POJ--3278 Catch That Cow
2015-08-06 17:17
302 查看
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
Sample Output
4
题意:额,大概意思就是农场主要去找他自己的牛,农场主在N行,牛在K行,农场主一次可以走+1,-1和2*N这三种行进路线,每一种都会花费1分钟,那么问题来了,农场主最快要几分钟能够捉到自己的牛。
注意:要注意特殊数据的考虑 比如0-100000或者100000-0一道比较正常的广搜。
ac代码:
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
题意:额,大概意思就是农场主要去找他自己的牛,农场主在N行,牛在K行,农场主一次可以走+1,-1和2*N这三种行进路线,每一种都会花费1分钟,那么问题来了,农场主最快要几分钟能够捉到自己的牛。
注意:要注意特殊数据的考虑 比如0-100000或者100000-0一道比较正常的广搜。
ac代码:
#include<stdio.h> #include<queue> #include<string.h> using namespace std; #define INF 0xfffffff int vis[100010]; int k,n,cn[2]={1,-1},ans; struct node{ int n,step; }a,temp; int jud(int x){ if(x>=0&&x<=100000&&vis[x]==0) return 1; return 0; } void bfs(int x){ queue<node>q; a.n=x; a.step=0; q.push(a); vis[a.n]=1; int i; while(!q.empty()){ a=q.front(); q.pop(); for(i=0;i<3;i++){ if(i==2) temp.n=a.n*2; else temp.n=a.n+cn[i]; temp.step=a.step+1; if(jud(temp.n)){ vis[temp.n]=1; if(temp.n==k){ if(ans>temp.step) ans=temp.step; continue ; } q.push(temp); } } } } int main(){ while(scanf("%d%d",&n,&k)!=EOF){ memset(vis,0,sizeof(vis)); ans=INF; if(n>=k) printf("%d\n",n-k); else{ bfs(n); printf("%d\n",ans); } } return 0; }
相关文章推荐
- Mysql中DATE_SUB 使用方法结合查询一天内,一周内,一月内的信息实例讲解
- 杭电(hdu)ACM 2034 人见人爱A-B
- WPF入门教程系列十五——WPF中的数据绑定(一)
- java中的log中的用法和小结
- hdu 4039 2011成都赛区网络赛I ***
- 优雅地使用CodeIgniter 3之Session类库(1)(转)
- 选择排序法(从大到小)
- the user specified as a definer('root'@'%') does not exit解决办法
- c++ builder 2009如何生成独立运行exe(转)
- Android开发java.io.FileNotFoundException:open failed: EACCES(Permission denied)
- 通往WinDbg的捷径
- UIImageView旋转动画
- ZOJ 2748 Free Kick
- TCP为何采用三次握手来建立连接,若采用二次握手可以吗?
- Tomcat性能优化(三) Executor配置
- web 发布
- CentOS 6.4下Percona Xtrabackup安装部署错误处理
- 杭电acm 2143 box
- 【华为oj】m个苹果方n个盘子,多少种方法?
- StrutsPrepareAndExecuteFilter的作用