AcWing 1100. 抓住那头牛
原题链接
简单
作者:
i_11
,
2021-04-15 17:02:12
,
所有人可见
,
阅读 286
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n,k;
int d[N];
int bfs()
{
queue<int> q;
q.push(n);
d[n] = 0;
while (q.size())
{
int t = q.front();
q.pop();
if (t == k) return d[t];
int dx[3];
dx[0] = t + 1;
dx[1] = t - 1;
dx[2] = t * 2;
for (int i = 0 ; i < 3; i ++)
{
int a = dx[i];
if (a >= 0 && a < N && d[a] == -1)
{
d[a] = d[t] + 1;
q.push(a);
}
}
}
}
int main()
{
memset(d,-1,sizeof d);
cin >> n >> k;
cout << bfs() << endl;
return 0;
}