D - Catch That Cow BFS

The farmer knows the location of a cow, you want to grab it. And cattle farmers are in logarithmic axis, farmer starting at point N (0 <= N <= 100000), bovine positioned at the point K (0 <= K <= 100000). The farmer has two movements: 1, to move from the X or X-1 X + 1, 2 each take a minute movement, to move from X 2 * X, take a minute movement every cow is assumed unaware farmer action, not standing still. How much time it takes to seize at least cow? Input

Line: the two letters separated by a space:  N  and  K

Output

Line: cattle farmer grabbed minimum time required in minutes

Sample Input

5 17

Sample Output

4

Hint

Use the shortest possible time to seize the cattle farmer's programs are as follows: 5-10-9-18-17, 4 minutes.
// use Long Long 
#include <the iostream> #include <CString> #include <Queue> the using namespace STD; typedef Long Long LL; LL n-, K; const int N = 100001 ; int ARR [N] = { 0 } ; // record the number of steps int B [N] = { 0 }; // tag array int A [ . 3 ] = { . 1 , - . 1 , 2 }; void the BFS ( int Start) { Queue <LL> S; S. push (start); // will start typing B [ Start] =0;//开始赋0 arr[start]=1;标记 while(s.size()){ ll q=s.front(); s.pop();
if(q==k){ break; } int x; for(int i=0;i<3;i++){ if(a[i]==2){ x=q*2; } else { x=q+a[i]; } if(X> = 0 && X <= N && ARR [X] == 0 ) { B [X] = B [Q] + . 1 ; // add a new s.push (X); ARR [X] = . 1 ; IF (x == k) reaches k // If x out BREAK ; } } } } int main () { the while (CIN >> >> n- k) { Memset (ARR, 0 , the sizeof (ARR)); the BFS ( n-); COUT << B [K] << endl; } }

 

Guess you like

Origin www.cnblogs.com/Accepting/p/11234807.html