Catch that Cowtime limit:4000/2000ms (java/other) Memory limit:131072/65536k (Java/other) total submission (s): 113 Accepted Submission (s): 46Problem 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 in a single minute
* TELEPORTING:FJ can move from any point x to the point 2x 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?
Inputline 1:two space-separated integers:
Nand
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
1#include <stdio.h>2#include <queue>3#include <string.h>4 using namespacestd;5 Const intmaxn=100010;6 intVISIT[MAXN];7 intnow[3];8 intn,k,step,t;9 voidBFs () {Tenqueue<int>DL; One Dl.push (N); Avisit[n]=1; - if(n!=K) { - while(!Dl.empty ()) { thet=dl.size (); -step++; - while(t--){ -now[0]=dl.front () +1; +now[1]=dl.front ()-1; -now[2]=dl.front () *2; + Dl.pop (); A for(intI=0;i<3; i++){ at if(now[i]==k)return; - if(now[i]>0&&now[i]<=100000&&!visit[now[i]]) {//drunk, a 0 less, wrong n times; ;;; -visit[now[i]]=1;d L.push (Now[i]); - } - } - } in } - } to } + intMain () { - while(~SCANF ("%d%d",&n,&K)) { thememset (Visit,0,sizeof(visit)); *step=0; $ BFS ();Panax Notoginsengprintf"%d\n", step); - } the return 0; +}
Catch that Cow