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.

43 lines
799 B

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