You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

30 lines
656 B

#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;
}