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.
35 lines
722 B
35 lines
722 B
#include <bits/stdc++.h>
|
|
|
|
using namespace std;
|
|
|
|
struct Node {
|
|
int number; //值
|
|
int cnt; //次数
|
|
};
|
|
const int N = 1e5 + 10;
|
|
bool st[N];
|
|
int main() {
|
|
int a, b;
|
|
cin >> a >> b;
|
|
queue<Node> q;
|
|
q.push({a, 0});
|
|
st[a] = true;
|
|
while (q.size()) {
|
|
auto t = q.front();
|
|
q.pop();
|
|
if (t.number == b) {
|
|
cout << t.cnt << endl;
|
|
break;
|
|
}
|
|
if (!st[t.number + 1]) {
|
|
st[t.number + 1] = true;
|
|
q.push({t.number + 1, t.cnt + 1});
|
|
}
|
|
if (!st[t.number * 2]) {
|
|
st[t.number * 2] = true;
|
|
q.push({t.number * 2, t.cnt + 1});
|
|
}
|
|
}
|
|
return 0;
|
|
}
|