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?
Input
Line 1:two space-separated integers:
Nand
K
Output
Line 1:the least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.
Sample Input
5 17
Sample Output
4
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.
Source
Usaco Open Silver Test instructions: given two integers n and k, by n+1 or n-1 or n*2 3 operations, make n==k, output the least number of operations BFS, the first group found is the minimum number of steps
#include <iostream>#include<cstdlib>#include<queue>#defineN 1000000using namespacestd;structnode{intX,step;}; Queue<node>Q;node First,now;BOOLvis[n+Ten];intN,k,ans;intCheckintx) { if(x<0|| X>n | | VIS[X])return 0; return 1;}voidExtend (nodeinch) {node P=inch; P.step++; P.x=inch. x*2; if(check (p.x)) vis[p.x] =true, Q.push (p); P.x=inch. x+1; if(check (p.x)) vis[p.x] =true, Q.push (p); P.x=inch. x-1; if(check (p.x)) vis[p.x] =true, Q.push (p);}intBFs () {Q.push (first); while(!Q.empty ()) { Now=Q.front (); Q.pop (); if(now.x = = k)returnNow.step; Extend (now); }}intMain () {CIN>>n>>K; First.x=N; cout<<BFS ();}
"poj3278" Catch that Cow