#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<queue>
#define N 100005
using namespace std;
struct node{
int x,y,step;
};
int n,m;
int vis[N];
int k;
int check(int x){
if(x<0||x>100000||vis[x])return 1;//写边界不要写成x>n,记得改,很久才发现这个bug
return 0;
}
int bfs(){
int i;
queue<node>Q;
node a,next;
a.x=n;
a.step=0;
memset(vis,0,sizeof(vis));
vis[a.x]=1;
Q.push(a);
while(!Q.empty()){
a=Q.front();
Q.pop();
if(a.x==k)return a.step;
next.x=a.x+1;
if(next.x==k)return a.step+1;
if(!check(next.x)){
next.step=a.step+1;
vis[next.x]=1;
Q.push(next);
}
next.x=a.x-1;
if(next.x==k)return a.step+1;
if(!check(next.x)){
next.step=a.step+1;
vis[next.x]=1;
Q.push(next);
}
next.x=2*a.x;
if(next.x==k)return a.step+1;
if(!check(next.x)){
next.step=a.step+1;
vis[next.x]=1;
Q.push(next);
}
}
return 0;
}
int main(){
while(scanf("%d%d",&n,&k)!=EOF){
printf("%d\n",bfs());
}
return 0;
}
hdu2717 Catch That Cow
最新推荐文章于 2022-10-16 04:57:01 发布