标签:
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?
1 #include<stdio.h> 2 #include<queue> 3 #include<string.h> 4 using namespace std; 5 const int MAXN=100010; 6 int visit[MAXN]; 7 int now[3]; 8 int N,K,step,t; 9 void bfs(){ 10 queue<int>dl; 11 dl.push(N); 12 visit[N]=1; 13 if(N!=K){ 14 while(!dl.empty()){ 15 t=dl.size(); 16 step++; 17 while(t--){ 18 now[0]=dl.front()+1; 19 now[1]=dl.front()-1; 20 now[2]=dl.front()*2; 21 dl.pop(); 22 for(int i=0;i<3;i++){ 23 if(now[i]==K)return; 24 if(now[i]>0&&now[i]<=100000&&!visit[now[i]]){//醉了,少了个0,错了n次;;;;; 25 visit[now[i]]=1;dl.push(now[i]); 26 } 27 } 28 } 29 } 30 } 31 } 32 int main(){ 33 while(~scanf("%d%d",&N,&K)){ 34 memset(visit,0,sizeof(visit)); 35 step=0; 36 bfs(); 37 printf("%d\n",step); 38 } 39 return 0; 40 }
标签:
原文地址:http://www.cnblogs.com/handsomecui/p/4709312.html