原题:
思路:用BFS来做,与二维数组些许不同
#include<iostream>
#include<queue>
using namespace std;
queue<int> q;
queue<int> t;
int ans = 0;
int cnt = 0;
int res = 0;
int bfs(int x)
{
q.push(x);
while (q.size())
{
ans++;
int i = 0, j = 0, k = 0;
while (q.size())
{
int l = q.front();
q.pop();
if (l % 2 == 0)
i = l / 2;
j = l + 1;
k = l - 1;
if (i == 1 || j == 1 || k == 1)
return ans;
if (i != 0)
t.push(i);
t.push(j);
t.push(k);
}
while (t.size())
{
q.push(t.front());
t.pop();
}
}
}
int main()
{
cout << bfs(2021) << endl;
return 0;
}