//#include<iostream> //#include<queue> //using namespace std; // //const int maxn=1e5+5; //struct point{ // int x; // int time; //}; // //struct point qs[maxn]; //int n,k; //bool map[maxn]; // //void find(int s,int t) //{ // //} // //int main() //{ // int n,k; // cin>>n>>k; // memset(map,true,sizeof(map)); // memset(qs,0,sizeof(qs)); // // // int cnt=0; //// if(n==0&&k>n) //// { //// n=n+1; //// cnt++; //// } //// int i=n; //// while(abs(k-i)>=2*i||abs(k-(2*i))<abs(k-i)) //// { //// i=i*2; //// cnt++; //// } //// cnt+=abs(k-i); //// cout<<cnt<<endl; //// return 0; // // // //} // #include<iostream> #include<queue> using namespace std; struct st { int loc; int t; }; const int maxn=1e5+5; queue<st> q; bool vis[maxn]; int main() { int n,k; cin>>n>>k; st start{n,0}; q.push(start); //int steps=0; vis[n]=true; while(!q.empty()) { st tmp=q.front(); if(tmp.loc==k) { cout<<tmp.t<<endl; break; } // else if(tmp.loc>k) // { // cout<<tmp.t+(tmp.loc-k)<<endl; // break; // } else { if(!vis[tmp.loc+1]&&tmp.loc+1<=maxn) { st right{tmp.loc+1,tmp.t+1}; q.push(right); vis[right.loc]=true; } if(!vis[tmp.loc-1]&&tmp.loc-1>=0) { st left{tmp.loc-1,tmp.t+1}; q.push(left); vis[left.loc]=true; } if(!vis[tmp.loc*2]&&tmp.loc*2<=maxn) { st prod2{tmp.loc*2,tmp.t+1}; q.push(prod2); vis[prod2.loc]=true; } q.pop(); } } return 0; } //------------------------上面是RE代码---------------------- //由&&引发的RE,无限RE,调了很长时间...tnnd //注意左右边界 //注意&& //注意bool数组的意义 #include<iostream> #include<cstdio> #include<queue> using namespace std; struct st { int loc; int t; }; const int maxn=1e5+5; queue<st> q; bool vis[maxn]; int main() { int n,k; cin>>n>>k; st start; start.loc=n; start.t=0; q.push(start); vis[start.loc]=true; while(!q.empty()) { st tmp; tmp=q.front(); if(tmp.loc==k) { cout<<tmp.t<<endl; break; } st node; if(tmp.loc+1<=maxn&&!vis[tmp.loc+1]) { node.loc=tmp.loc+1; node.t=tmp.t+1; q.push(node); vis[node.loc]=true; } if(tmp.loc-1>=0&&!vis[tmp.loc-1]) { node.loc=tmp.loc-1; node.t=tmp.t+1; q.push(node); vis[node.loc]=true; } if(tmp.loc*2<=maxn&&!vis[tmp.loc*2]) { node.loc=tmp.loc*2; node.t=tmp.t+1; q.push(node); vis[node.loc]=true; } q.pop(); } return 0; }