AcWing 1100. 抓住那头牛
原题链接
简单
作者:
hai阿卢
,
2021-03-08 21:03:55
,
所有人可见
,
阅读 272
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
const int N = 1e5 + 10;
int a[N], d[N];
bool s[N];
queue<int> q;
int Bfs(int n, int m)
{
q.push(n);
s[n] = true;
d[n] = 0;
while(!q.empty())
{
int it = q.front();
q.pop();
int i = it + 1;
if(i > 0 && i < N && !s[i])
{
s[i] = true;
d[i] = d[it] + 1;
q.push(i);
}
i = it - 1;
if(i > 0 && i < N && !s[i])
{
s[i] = true;
d[i] = d[it] + 1;
q.push(i);
}
i = it * 2;
if(i > 0 && i < N && !s[i])
{
s[i] = true;
d[i] = d[it] + 1;
q.push(i);
}
}
return d[m];
}
int main()
{
int n, m;
cin >> n >> m;
memset(d, 0x3f, sizeof(d));
for(int i = 1; i <= m || i <= n; i++) a[i] = i;
cout << Bfs(n, m) << endl;
return 0;
}