| Catch that Cow |
| Time limit:5000/2000 MS (java/others) Memory limit:32768/32768 K (java/others) |
| Total submission (s): 243 Accepted Submission (s): 88 |
|
Problem Description Farmer 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 Hint The fastest-Farmer John to reach the fugitive cow are to move along the following PATH:5-10-9-18-17, which takes 4 Minutes. |
|
| Sourceusaco Open Silver |
| Recommendteddy |
#include <bits/stdc++.h>#defineN 5000100#defineINF 0x3f3f3f3fusing namespacestd;intn,m;structnode{intX,val; Node () {} node (intAintb) {x=A; Val=C; }};BOOLVis[n];intBFs () {Queue<node>Q; Q.push (node (n,0)); Vis[n]=true; while(!Q.empty ()) {Node Fr=Q.front (); Q.pop (); //cout<<fr.x<<endl; if(fr.x==m) {//cout<< "x=" <<fr.x<<endl; returnFr.val; } intans=fr.x+1; if(ans>=0&&ans<=100000&&!Vis[ans]) {Q.push (node (ans,fr.val+1)); Vis[ans]=true; } ans=fr.x-1; if(ans>=0&&ans<=100000&&!Vis[ans]) {Q.push (node (ans,fr.val+1)); Vis[ans]=true; } ans=fr.x*2; if(ans>=0&&ans<=100000&&!Vis[ans]) {Q.push (node (ans,fr.val+1)); Vis[ans]=true; } } //return-1;}intMain () {//freopen ("C:\\users\\acer\\desktop\\in.txt", "R", stdin); while(SCANF ("%d%d", &n,&m)! =EOF) {memset (Vis,false,sizeofvis); printf ("%d\n", BFS ()); } return 0;}
Catch that Cow