#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
const int maxn=200000+10;
int f[maxn],vis[maxn];
using namespace std;
int main()
{
memset(f,0,sizeof(f));
int n,k,ans,i,j,h;
scanf("%d%d",&n,&k);
queue<int> q;
while(!q.empty()) q.pop();
q.push(n);
while(!q.empty())
{
ans=q.front();
if(ans==k) {printf("%d\n",f[ans]);break;}
i=ans-1;
if(!vis[i]&&i>=0&&i<=100000){
vis[i]=1;
f[i]=f[ans]+1;
q.push(i);}
j=ans+1;
if(!vis[j]&&j>=0&&j<=100000){
vis[j]=1;
f[j]=f[ans]+1;
q.push(j);}
h=ans*2;
if(!vis[h]&&h>=0&&h<=100000){
vis[h]=1;
f[h]=f[ans]+1;
q.push(h);}
q.pop();
}
return 0;
}
POJ 3278 Catch That Cow
最新推荐文章于 2022-06-25 17:20:30 发布