本文主要是介绍poj_3278_Catch That Cow_201407241728,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 45959 | Accepted: 14400 |
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
Output
Sample Input
5 17
Sample Output
4
Hint
Source
经过+1,-1,*2的操作,使第一个数等于第二个数
求最少步骤都是用的广搜
#include <stdio.h>
#include <string.h>
#include <queue>using namespace std;const int MAXN = 100010;int vis[MAXN],step[MAXN];
int n,k;
queue<int> q;int bfs(int n,int k)
{int i,head,next; step[n]=0;vis[n]=1;q.push(n);while(!q.empty()){head=q.front();q.pop();for(i=0;i<3;i++){if(i==0)next=head-1;else if(i==1)next=head+1;elsenext=head*2;if(next>MAXN||next<0)continue;if(!vis[next]){vis[next]=1;q.push(next);step[next]=step[head]+1;}if(k==next)return step[next];}}
}
int main()
{ while(scanf("%d%d",&n,&k)!=EOF){memset(vis,0,sizeof(vis));if(n>=k)printf("%d\n",n-k);elseprintf("%d\n",bfs(n,k));}return 0;
}
这篇关于poj_3278_Catch That Cow_201407241728的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!