#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <algorithm>
#define ll long long
using namespace std;
int n,k,vis[100005];
struct node
{
int x,step;
};
int bfs()
{
vis[n]=1;
queue<node> q;
q.push({n,0});
while(!q.empty())
{
node t=q.front();
q.pop();
if(t.x==k)
return t.step;
if(vis[t.x-1]==0)
{
vis[t.x-1]=1;
q.push({t.x-1,t.step+1});
}
if(vis[t.x+1]==0)
{
vis[t.x+1]=1;
q.push({t.x+1,t.step+1});
}
if(vis[t.x*2]==0)
{
vis[t.x*2]=1;
q.push({t.x*2,t.step+1});
}
}
return 0;
}
int main()
{
ios::sync_with_stdio(false);
cin >> n >> k;
cout << bfs() << endl;
return 0;
}
POJ 4001 抓住那头牛(BFS 最短路径)
最新推荐文章于 2024-03-31 12:10:12 发布