'commit'
This commit is contained in:
30
TangDou/Bfs/Test.cpp
Normal file
30
TangDou/Bfs/Test.cpp
Normal file
@@ -0,0 +1,30 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
int a, b;
|
||||
const int N = 10010;
|
||||
bool st[N];
|
||||
typedef pair<int, int> PII;
|
||||
void bfs() {
|
||||
queue<PII> q; // q.first 当前是哪个数 q.second 从出发点到q.first走了几步
|
||||
q.push({a, 0});
|
||||
st[a] = true;
|
||||
while (q.size()) {
|
||||
auto t = q.front();
|
||||
q.pop();
|
||||
int u = t.first;
|
||||
int d = t.second;
|
||||
if (u == b) {
|
||||
cout << d << endl;
|
||||
exit(0);
|
||||
}
|
||||
if (!st[u + 1]) q.push({u + 1, d + 1});
|
||||
if (!st[u * 2]) q.push({u * 2, d + 1});
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
cin >> a >> b;
|
||||
bfs();
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user