Files
python/TangDou/LanQiaoBei/LanQiao13STEMA202210/4.cpp

43 lines
799 B
C++
Raw Normal View History

2025-08-30 18:35:01 +08:00
#include <bits/stdc++.h>
using namespace std;
struct Node {
int num;
int step;
};
const int N = 1010;
int n, m;
queue<Node> q;
int st[N];
void bfs() {
q.push({n, 0});
while (q.size()) {
auto x = q.front();
q.pop();
if (x.num == m) {
printf("%d\n", x.step);
break;
}
if (!st[x.num + 1]) {
q.push({x.num + 1, x.step + 1});
st[x.num + 1] = 1;
}
if (!st[x.num - 1]) {
q.push({x.num - 1, x.step + 1});
st[x.num - 1] = 1;
}
if (!st[x.num * 2]) {
q.push({x.num * 2, x.step + 1});
st[x.num * 2] = 1;
}
}
}
int main() {
cin >> n >> m;
bfs();
return 0;
}