View Code
1 //zzuli1220 2 #include<stdio.h> 3 #include<queue> 4 using namespace std; 5 int n,k; 6 int f[100001]; 7 bool a[100001]; 8 queue<int>q; 9 void bfs() 10 { 11 int x; 12 a[n]=1; 13 q.push(n); 14 while(!q.empty()) 15 { 16 x=q.front(); 17 q.pop(); 18 if(x==k) 19 break; 20 21 if(x-1>=0&&!a[x-1]) 22 { 23 a[x-1]=1; 24 q.push(x-1); 25 f[x-1]=f[x]+1; 26 } 27 if(x+1<=100000&&!a[x+1]) 28 { 29 a[x+1]=1; 30 q.push(x+1); 31 f[x+1]=f[x]+1; 32 } 33 if(x*2<=100000&&!a[x*2]) 34 { 35 a[x*2]=1; 36 q.push(x*2); 37 f[x*2]=f[x]+1; 38 } 39 } 40 41 } 42 int main() 43 { 44 scanf("%d%d",&n,&k); 45 bfs(); 46 printf("%d\n",f[k]); 47 return 0; 48 }