'commit'
This commit is contained in:
42
TangDou/Bfs/2_LanQiaoBeiSai13_1000_1.cpp
Normal file
42
TangDou/Bfs/2_LanQiaoBeiSai13_1000_1.cpp
Normal file
@@ -0,0 +1,42 @@
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
typedef pair<int, int> PII;
|
||||
const int N = 1010;
|
||||
bool st[N];
|
||||
bool check(int x) {
|
||||
if (x < 1 || x > 1000) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
int n, m;
|
||||
void bfs() {
|
||||
queue<PII> q;
|
||||
q.push({n, 0});
|
||||
st[n] = true;
|
||||
while (q.size()) {
|
||||
auto x = q.front();
|
||||
q.pop();
|
||||
if (x.first == m) {
|
||||
printf("%d", x.second);
|
||||
break;
|
||||
}
|
||||
if (!st[x.first + 1] && check(x.first + 1)) {
|
||||
q.push({x.first + 1, x.second + 1});
|
||||
st[x.first + 1] = true;
|
||||
}
|
||||
if (!st[x.first - 1] && check(x.first - 1)) {
|
||||
q.push({x.first - 1, x.second + 1});
|
||||
st[x.first - 1] = true;
|
||||
}
|
||||
if (!st[x.first * 2] && check(x.first * 2)) {
|
||||
q.push({x.first * 2, x.second + 1});
|
||||
st[x.first * 2] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
cin >> n >> m;
|
||||
bfs();
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user