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.
python/TangDou/Bfs/2_LanQiaoBeiSai13_1000_2.cpp

34 lines
604 B

#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int n, m;
//结构体
struct Node {
int num;
int step;
};
//队列中装的是结构体
queue<Node> q;
void bfs() {
//形成一个数对放入队列
q.push({n, 0});
//套路
while (q.size()) {
auto x = q.front();
q.pop();
if (x.num == m) {
printf("%d", x.step);
break;
}
q.push({x.num + 1, x.step + 1});
q.push({x.num - 1, x.step + 1});
q.push({x.num * 2, x.step + 1});
}
}
int main() {
cin >> n >> m;
bfs();
return 0;
}