嗯……很多博客都做了详细介绍和解释。
http://blog.youkuaiyun.com/freezhanacmore/article/details/8168265
等等
#include<map>
#include<set>
#include<list>
#include<stack>
#include<deque>
#include<queue>
#include<vector>
#include<sstream>
#include<iomanip>
#include<iostream>
#include<math.h>
#include<ctype.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#include<functional>
#define CPY(A,B)memcpy(A,B,sizeof(A))
#define fin freopen("in.txt","r",stdin)
#define fout freopen("out.txt","w",stdout)
typedef long long LL;
typedef unsigned long long uLL;
const int MOD=1e9+7;
const int INF=0x3f3f3f3f;
const LL INFF=0x3f3f3f3f3f3f3f3fLL;
const double OO=1e20;
const double EPS=1e-9;
const double PI=acos (-1.0);
int dx[]= {0,1,0,-1};
int dy[]= {1,0,-1,0};
using namespace std;
int gcd (const LL &a,const LL &b) {
return b==0?a:gcd (b,a%b);
}
inline int dcmp (double a,double b) {
if (fabs (a-b) <EPS) { return 0; }
return a<b?-1:1;
}
const int M=200200;
int n,k,ans,vis[M];
void SS () {
queue<int> Q;
Q.push (n);
vis[n]=1;
while (!Q.empty() ) {
int now=Q.front();
Q.pop();
if (now==k) {
printf ("%d\n",vis[now]-1);
return;
}//分三个方向
if ( (now*2) <M&&!vis[now*2]) {
Q.push (now*2);
vis[now*2]=vis[now]+1;
}
if ( (now+1) <M&&!vis[now+1]) {
Q.push (now+1);
vis[now+1]=vis[now]+1;
}
if ( (now-1) >=0&&!vis[now-1]) {
Q.push (now-1);
vis[now-1]=vis[now]+1;
}
}
}
int main() {
while (cin>>n>>k) {
memset (vis,0,sizeof vis);
SS ();
}
return 0;
}