poj 3278 Catch That Cow
来源:互联网 发布:insider.windows.com 编辑:程序博客网 时间:2024/06/06 14:11
Catch That Cow
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 43187 Accepted: 13435
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+优先队列
#include"stdio.h"#include"string.h"#include"queue"using namespace std;#define N 100005int mark[N],k,n;struct node{int x,step;friend bool operator<(node a,node b){return a.step>b.step ; //用时少的优先级高}};int judge(int x){if(x>=0&&(x<=n||x<k+2)&&!mark[x]) //N可能远大于K,故x<=k+2不对哦!!return 1;return 0;}int bfs(int x){priority_queue<node>q;node cur,next;cur.x=x;cur.step=0;q.push(cur);mark[x]=1;while(!q.empty()){cur=q.top();if(cur.x==k)return cur.step;q.pop();next.step=cur.step+1; next.x=cur.x+1;//+1if(judge(next.x)){mark[next.x]=1;q.push(next);}next.x=cur.x-1; //-1if(judge(next.x)){mark[next.x]=1;q.push(next);}next.x=cur.x*2; //*2if(judge(next.x)){mark[next.x]=1;q.push(next);}}return 0;}int main(){while(scanf("%d%d",&n,&k)!=-1){memset(mark,0,sizeof(mark));printf("%d\n",bfs(n));}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
- FTP上传文件说明
- hdu 1171 Big Event in HDU --- 多重背包
- IMEI
- Js中 关于top、clientTop、scrollTop、offsetTop的用法
- 职场上成功人士必备的能力有哪些
- poj 3278 Catch That Cow
- jQuery - AJAX get() 和 post() 方法
- Floyd判圈算法(龟兔赛跑算法)【模板】
- 现诚心hi
- Spring--quartz中cronExpression配置说明
- Hibernate 多表映射(Mybatis,Springjdbc,Hibernate ,JDBC的多表映射学习比较)
- Linux 杂记
- TLD动态跟踪系统中的学习策略—P-N Learning
- iOS开发之UIWebView