#include <iostream>
#include <queue>
#include <cstring>
#define maxn 100005
using namespace std;
int visit[maxn]={0};
struct stu
{ int x,s;
};
int re;
void BFS(int a,int b)
{
memset(visit,0,sizeof(visit));
queue<stu>Find;
stu xx,yy;
xx.x=a;xx.s=0;
Find.push(xx);
while(Find.size())
{
xx=Find.front();
Find.pop();
if(xx.x==b){re=xx.s;return;}
else
{
yy.x=xx.x+1;
yy.s=xx.s+1;
if(!visit[yy.x]&&yy.x>=0&&yy.x<maxn)visit[yy.x]=1,Find.push(yy);
yy.x=xx.x-1;
if(!visit[yy.x]&&yy.x>=0&&yy.x<maxn)visit[yy.x]=1,Find.push(yy);
yy.x=xx.x*2;
if(!visit[yy.x]&&yy.x>=0&&yy.x<maxn)visit[yy.x]=1,Find.push(yy);
}
}
}
int main()
{
int n,m;
while(cin>>n>>m)
{
if(n==m)
cout<<"0"<<endl;
else
{
BFS(n,m);cout<<re<<endl;
}
}
return 0;
}
杭电2717
最新推荐文章于 2018-07-30 23:30:37 发布