https://www.cnblogs.com/yspworld/p/4046684.html
终于AC了 注意利用tmp。。。
#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
bool vis[100100];
int n,k,ans,X,N;
struct farmer
{
int step,weizhi;
};
queue<farmer>Q;
int BFS()
{
farmer fa,tmp;
fa.weizhi=n;
fa.step=0;
vis[X]=true;
Q.push(fa);
while(!Q.empty())
{
fa=Q.front();
Q.pop();
X=fa.weizhi;
N=fa.step;
if(X==k)
{
return N;
}
if(X-1>=0&&vis[X-1]==0)
{
tmp.weizhi=X-1;
tmp.step=N+1;
vis[X-1]=true;
Q.push(tmp);
}
if(X+1<=100000&&vis[X+1]==0)
{
tmp.weizhi=X+1;
tmp.step=N+1;
vis[X+1]=true;
Q.push(tmp);
}
if(X*2<=100000&&vis[X*2]==0)
{
tmp.weizhi=X*2;
tmp.step=N+1;
vis[X*2]=true;
Q.push(tmp);
}
}
return 0;
}
int main()
{
while(~scanf("%d%d",&n,&k))
{
while(!Q.empty()){Q.pop();}
memset(vis,0,sizeof(vis));
ans=0;
if(n>k)
{
cout<<n-k<<endl;
continue;
}
ans=BFS();
cout<<ans<<endl;
}
}