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