'commit'
This commit is contained in:
47
TangDou/Bfs/2_LanQiaoBeiSai13_1000_4.cpp
Normal file
47
TangDou/Bfs/2_LanQiaoBeiSai13_1000_4.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
typedef pair<int, int> PII;
|
||||
const int N = 1010;
|
||||
int n, m;
|
||||
queue<PII> q;
|
||||
int st[N];
|
||||
//检查是不是出界
|
||||
bool check(int pos) {
|
||||
if (pos < 1 || pos > 1000) return false;
|
||||
return true;
|
||||
}
|
||||
void bfs() {
|
||||
//形成一个数对放入队列
|
||||
q.push({n, 0});
|
||||
//套路
|
||||
while (q.size()) {
|
||||
PII x = q.front();
|
||||
q.pop();
|
||||
if (x.first == m) {
|
||||
cout << x.second << endl;
|
||||
break;
|
||||
}
|
||||
if (!st[x.first + 1]) {
|
||||
if (!check(x.first + 1)) continue;
|
||||
q.push({x.first + 1, x.second + 1});
|
||||
st[x.first + 1] = 1;
|
||||
}
|
||||
if (!st[x.first - 1]) {
|
||||
if (!check(x.first - 1)) continue;
|
||||
q.push({x.first - 1, x.second + 1});
|
||||
st[x.first - 1] = 1;
|
||||
}
|
||||
if (!st[x.first * 2]) {
|
||||
if (!check(x.first * 2)) continue;
|
||||
q.push({x.first * 2, x.second + 1});
|
||||
st[x.first * 2] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
cin >> n >> m;
|
||||
bfs();
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user