AcWing 1100. 抓住那头牛
原题链接
简单
作者:
xhQYm
,
2020-02-14 21:23:01
,
所有人可见
,
阅读 678
C++ 代码
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
int n,k,vis[100005];
void bfs()
{
queue<int> q;
q.push(n);
while(!q.empty())
{
int cur=q.front();
q.pop();
if(cur==k)
cout<<vis[k];
if(cur+1<=100000 and !vis[cur+1])
{
vis[cur+1]=vis[cur]+1;
q.push(cur+1);
}
if(cur-1>=0 and !vis[cur-1])
{
vis[cur-1]=vis[cur]+1;
q.push(cur-1);
}
if(cur*2<=100000 and !vis[cur*2])
{
vis[cur*2]=vis[cur]+1;
q.push(cur*2);
}
}
}
int main()
{
cin>>n>>k;
bfs();
return 0;
}