Files
python/TangDou/XiTi/1384.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

32 lines
672 B
C++

#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;
}