'commit'
This commit is contained in:
43
TangDou/LanQiaoBei/LanQiao13STEMA202210/4.cpp
Normal file
43
TangDou/LanQiaoBei/LanQiao13STEMA202210/4.cpp
Normal file
@@ -0,0 +1,43 @@
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user