#include<iostream> #include<cstring> #include<queue> using namespace std; int n,k; int f[3]={-1,1,0}; int v[100005]; struct node{ int p; int s; }; int bfs(){ node x; x.p=n; x.s=0; v[n]=1; queue<node>q; q.push(x); while(!q.empty()){ for(int i=0;i<3;++i){ int xx; if(i!=2) xx=q.front().p+f[i]; else xx=q.front().p*2; if(xx>0&&xx<100004&&v[xx]==0){ node sp; sp.s=q.front().s+1; sp.p=xx; v[xx]=1; if(xx==k) return sp.s; else q.push(sp); } } q.pop(); } return 0; } int main(){ while(cin>>n>>k){ memset(v,0,sizeof(v)); cout<<bfs()<<endl; } } |
Double click to view unformatted code.