Catch that Cow
Time limit:5000/2000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 10166 Accepted Submission (s): 3179
Problem Descriptionfarmer John had 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 are at a point K (0≤k≤100,000) on the same number Line. Farmer John has modes of transportation:walking and teleporting.
* WALKING:FJ can move from any point x to the points X-1 or X + 1 inch a single minute
* TELEPORTING:FJ can move from any point X to the point 2xX 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?
Inputline 1:two space-separated integers:n and K
Outputline 1:the least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.
Sample INPUT5 17
Sample Output4
HintThe fastest-Farmer John to reach the fugitive cow are to move along the following PATH:5-10-9-18-17, which takes 4 Minutes.
1#include <cstdio>2#include <iostream>3#include <queue>4#include <cstring>5 using namespacestd;6 intb,e;7 intMintim;8 structnode9 {Ten intpos,t; One }k,tem; A intvis[100000+Ten]; -Queue<node>s; - voidBFS () the { - while(!s.empty ()) - S.pop (); -k.pos=b,k.t=0; + S.push (k); - while(!s.empty ()) + { Ak=S.front (); at S.pop (); - if(k.pos==e) - { -mintim=k.t; - return; - } in if(k.pos<0|| K.pos>100000|| Vis[k.pos])Continue; -vis[k.pos]=1; totem.t=k.t+1; +tem.pos=k.pos+1; - S.push (TEM); thetem.pos=k.pos-1; * S.push (TEM); $tem.pos=k.pos*2;Panax Notoginseng S.push (TEM); - } the } + intMain () A { the inti,j; +Freopen ("In.txt","R", stdin); - while(SCANF ("%d%d", &b,&e)! =EOF) $ { $memset (Vis,0,sizeof(Vis)); - BFS (); -printf"%d\n", Mintim); the } - return 0;Wuyi}
Catch that Cow (BFS)