题目描述
blablabla
样例
blablabla
算法1
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
#include<iostream>
#include<queue>
using namespace std;
const int N = 200010;
int n, m;
bool st[N];
int bfs(int n, int m)
{
queue<int> q;
q.push(n);
st[n] = true;
int res = -1;
while(q.size())
{
int size = q.size();
res ++;
while(size --)
{
auto t = q.front();
q.pop();
if (t == m) return res;
if (t + 1 < N && !st[t + 1])
{
q.push(t + 1);
st[t + 1] = true;
}
if (t - 1 >= 0 && !st[t - 1])
{
q.push(t - 1);
st[t - 1] = true;
}
if (t * 2 < N && !st[t * 2])
{
q.push(t * 2);
st[t * 2] = true;
}
}
}
return -1;
}
int main()
{
cin >> n >> m;
cout << bfs(n, m);
return 0;
}
算法2
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
blablabla