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.
32 lines
672 B
32 lines
672 B
#include <bits/stdc++.h>
|
|
|
|
using namespace std;
|
|
const int N = 100000;
|
|
struct Node {
|
|
int pos;
|
|
int step;
|
|
};
|
|
|
|
int main() {
|
|
int n, k; //农夫位置,牛的位置
|
|
cin >> n >> k;
|
|
|
|
queue<Node> q;
|
|
q.push({n, 0}); //农夫位置,步数
|
|
|
|
while (q.size()) {
|
|
auto t = q.front();
|
|
q.pop();
|
|
//出口
|
|
if (t.pos == k) {
|
|
cout << t.step << endl;
|
|
break;
|
|
}
|
|
if (t.pos + 1 <= N) q.push({t.pos + 1, t.step + 1});
|
|
if (t.pos - 1 >= 0) q.push({t.pos - 1, t.step + 1});
|
|
if (t.pos <= t.pos * 2) q.push({t.pos * 2, t.step + 1});
|
|
}
|
|
|
|
return 0;
|
|
}
|