Description
Farmer John has 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 is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two 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 2 × 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
Output
Sample Input
5 17
Sample Output
4
Hint
Source


1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<queue> 5 struct Node{ 6 int x,step; 7 Node (int xx = 0,int xstep = 0) : x(xx),step(xstep){} 8 }; 9 using namespace std; 10 int bfs(int n,int k){ 11 Node tmp; 12 int t,vis[100005]; 13 memset(vis,false,sizeof(vis)); 14 queue <Node> Q; 15 Q.push(Node(n,0)); 16 vis[n] = true; 17 while(!Q.empty()){ 18 tmp = Q.front(); 19 Q.pop(); 20 if(tmp.x == k) 21 return tmp.step; 22 for(int i = 0;i < 3;i++){ 23 if(i == 0){ 24 t = tmp.x - 1; 25 if(t >= 0 && t <= 100000 && vis[t] == false){//设置取值范围!! 26 vis[t] = true; 27 Q.push(Node(t,tmp.step + 1)); 28 } 29 } 30 else if(i == 1){ 31 t = tmp.x + 1; 32 if(t >= 0 && t <= 100000 && vis[t] == false){ 33 vis[t] = true; 34 Q.push(Node(t,tmp.step + 1)); 35 } 36 } 37 else{ 38 t = tmp.x * 2; 39 if(t >= 0 && t <= 100000 && vis[t] == false){ 40 vis[t] = true; 41 Q.push(Node(t,tmp.step + 1)); 42 } 43 } 44 } 45 } 46 return 0; 47 } 48 int main(){ 49 int n,k; 50 scanf("%d%d",&n,&k); 51 printf("%d\n",bfs(n,k)); 52 return 0; 53 }