超级简单的一维的BFS搜索,1Y,注意一点,就是不用考虑当前人的位置是在牛的前面还是后面,有的时候即便是在后面也可能需要继续向后挪动
//leehaoze
#include <iostream>
#include <deque>
#include <string>
#include <vector>
#include <queue>
#include <cstdio>
#include <stack>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <cstdio>
#include <cmath>
#include <cstdlib>
using namespace std;
const int INF = 1 << 29;
#define INC_SAT(val) (val = ((val)+1 > (val)) ? (val)+1 : (val))
#define ARR_SIZE(a) ( sizeof( (a) ) / sizeof( (a[0]) ) )
#define ULL unsigned long long
#define MAXN 100000 + 5
struct Pos {
Pos(int x, int step) : x_(x), step_(step) {}
Pos() : step_(0) {}
int x_, step_;
};
int N, K;
bool book[MAXN];
int Move[] = {1, -1};
void Input() {
scanf("%d%d", &N, &K);
memset(book, false, sizeof book);
}
bool Legal(int x) {
return x >= 0 && x <= 100000 && !book[x];
}
void BFS() {
queue<Pos> Q;
Q.push(Pos(N, 0));
while (!Q.empty()) {
Pos now = Q.front();
Q.pop();
if (now.x_ == K) {
cout << now.step_ << endl;
return;
}
int dx = now.x_ * 2;
if (Legal(dx)) {
book[dx] = true;
Q.push(Pos(dx, now.step_ + 1));
}
for (int i = 0; i < 2; ++i) {
dx = now.x_ + Move[i];
if (Legal(dx)) {
book[dx] = true;
Q.push(Pos(dx, now.step_ + 1));
}
}
}
}
int main() {
#ifdef LOCAL
freopen("IN.txt", "r", stdin);
#endif
std::ios::sync_with_stdio(false);
Input();
BFS();
}