题意:
给定正整数,代表两个人的位置。其中位置可以如下变化:
- 变成
- 变成
- 变成
问最少变化几次,能够变成。
思路:
看到这个题,我先想到能不能求解,但是这里有个的操作,意味着这样无法保证需要的“无后效性”。
所以就只好考虑搜索了,这里我采用了,因为是一层一层的遍历,而是递归到底层,虽然总体来说复杂度是一样的,但是容易爆栈,因为这题的达到了的级别。
于是就可以开一个数组,记录走到这个点花了多少步,同时这个数组也有标记走没走过的作用。然后就是一步步的拓展了。
代码:
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e6 + 5, mod = 998244353;
int f[N];
void solve()
{
int n, k;
cin >> n >> k;
queue<int> q;
q.push(n);
f[n] = 1;
while(!q.empty()){
int x = q.front();
q.pop();
if (x == k) break;
if (!f[x + 1]){
q.push(x + 1);
f[x + 1] = f[x] + 1;
}
if (x - 1 >= 0 && !f[x - 1]){
q.push(x - 1);
f[x - 1] = f[x] + 1;
}
if (x * 2 <= 2 * k && !f[2 * x]){
q.push(x * 2);
f[2 * x] = f[x] + 1;
}
}
cout << f[k] - 1;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int tt = 1;
// cin >> tt;
while (tt--) solve();
return 0;
}