Files
python/TangDou/Bfs/Test.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

30 lines
656 B
C++

#include <bits/stdc++.h>
using namespace std;
int a, b;
const int N = 10010;
bool st[N];
typedef pair<int, int> PII;
void bfs() {
queue<PII> q; // q.first 当前是哪个数 q.second 从出发点到q.first走了几步
q.push({a, 0});
st[a] = true;
while (q.size()) {
auto t = q.front();
q.pop();
int u = t.first;
int d = t.second;
if (u == b) {
cout << d << endl;
exit(0);
}
if (!st[u + 1]) q.push({u + 1, d + 1});
if (!st[u * 2]) q.push({u * 2, d + 1});
}
}
int main() {
cin >> a >> b;
bfs();
return 0;
}