#include<cstdio>
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<map>
#include<vector>
#include<queue>
#include<set>
using namespace std;
int biao[100010]={0};//用来标记
struct node{//关于点的状态
int x;//位置
int step;//走到这个点的步数
node(int xx,int y):x(xx),step(y){}//构建函数,用来将结构体中node中的x,step,一起放入栈中
};
void bfs(int n,int k)
{
queue<node>q;//用来储存位置,和步数的队列
q.push(node(n,0));//把开始时位置为n,此时步数为0,放入栈中
biao[n]=1;//该点走过,标记一下
while(!q.empty())//队列不为空,循环进行下去,扩展新的点,(刚开始写成了if~~~,一直不输出)
{
node now=q.front();取出队列最前面的点(先进先出)
q.pop();//该点弹出
if(now.x==k)//达到目的
{
cout<<now.step<<endl;//输出步数(肯定是最短的那个)
return;
}
//判断下一步的点是否走过,且能走
if(now.x-1>=0&&biao[now.x-1]==0)
{
q.push(node(now.x-1,now.step+1));
biao[now.x-1]=1;
}
if(now.x+1<=k&&biao[now.x+1]==0)
{
q.push(node(now.x+1,now.step+1));
biao[now.x+1]=1;
}
if(now.x<=k&&now.x*2<=100000&&biao[2*now.x]==0)//这有一个越界条件,now.x*2<=100000,一定是要<=,=不能省
{
q.push(node(now.x*2,now.step+1));
biao[now.x*2]=1;
}
}
return;//如果队列为空,还没有达到目标则无解,有无都可
}
int main()
{
int n,k;
cin>>n>>k;
bfs(n,k);
return 0;
}
07-31
581
09-14
09-14