#include#include#includeusingnamespacestd;constintMAXN=100010;intstep[MAXN],vis[MAXN];
queueQ;intBFS(intn,intk)
{intnext,head;
step[n]=0;
vis[n]=1;
Q.push(n);while(!Q.empty())
{
head=Q.front();
Q.pop();for(inti=0;i<3;i++)
{if(i==0) next=head-1;elseif(i==1) next=head+1;elsenext=head*2;if(next>MAXN || next<0)continue;if(!vis[next])
{
vis[next]=1;
Q.push(next);
step[next]=step[head]+1;
}if(k==next)returnstep[next];//广搜搜索的深度第一次相等的就是深度最小的那个支结点,所以没必要再比较哪个最少了}
}
}intmain()
{intn,k;while(scanf("%d%d",&n,&k)!=EOF)
{
memset(vis,0,sizeof(vis));if(n>=k) printf("%d\n",n-k);elseprintf("%d\n",BFS(n,k));
}return0;
}
网友评论