AcWing 1100. $$\color{navy}{\Huge抓住那头牛}$$
原题链接
简单
作者:
Anohkx
,
2023-10-01 18:29:21
,
所有人可见
,
阅读 71
#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
const int N = 2e5;
typedef pair<int, int> PII;
bool st[N];
int n, k;
inline int bfs(int x)
{
queue<PII> q;
q.push({x, 0});
while(!q.empty())
{
PII t = q.front();
q.pop();
if(t.x == k) return t.y;
if(!st[t.x + 1] && t.x + 1 <= 1e5 )
{
q.push({t.x + 1, t.y + 1});
st[t.x + 1] = 1;
}
if(!st[t.x - 1] && t.x - 1 >= 0)
{
q.push({t.x - 1, t.y + 1});
st[t.x - 1] = 1;
}
if(!st[t.x * 2] && t.x * 2 <= 1e5)
{
q.push({t.x * 2, t.y + 1});
st[t.x * 2] = 1;
}
}
}
int main()
{
cin >> n >> k;
cout << bfs(n);
return 0;
}