#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
struct node
{
int x,step;
};
const int M=200010;
int visit[M];
int bfs(int start, int aim)
{
node v[M];
node a,b;
a.x=start;
a.step=0;
visit[start]=1;
v[0]=a;
int zz=1,zx=0;
while( zz-zx>=1 )
{
a=v[zx];
zx++;
if( a.x==aim )
return a.step;
for(int i=0;i<3;i++)
{
if(i==0 && a.x-1>=0 && a.x<=aim+1 )
b.x=a.x-1;
else if(i==1 && a.x<=aim)
b.x=a.x+1;
else if(i==2 && a.x<=aim)
b.x=a.x*2;
else
continue;
b.step=a.step+1;
if( visit[b.x]==0 && b.x<=2*aim )
{
v[zz++]=b;
visit[b.x]=1;
}
}
}
}
int main()
{
// freopen("in","r",stdin);
// freopen("out","w",stdout);
int n,k,i;
while(scanf("%d%d",&n,&k)!=EOF)
{
memset(visit,0,sizeof(visit));
if(n>k)
printf("%d\n",n-k);
else
printf("%d\n",bfs(n,k));
}
return 0;
}
poj3278 BFS
最新推荐文章于 2020-03-08 22:05:43 发布