#include <bits/stdc++.h>
using namespace std;
int dist[100002];
int main(void) {
ios::sync_with_stdio(0);
cin.tie(0);
int n, k;
queue < int > q;
cin >> n >> k;
fill(dist, dist + 100001, -1);
dist[n] = 0;
q.push(n);
if (2 * n <= 100000) { // new
dist[2 * n] = 0;
q.push(2 * n);
}
while (!q.empty()) {
int cur = q.front();
q.pop();
int target = 2 * cur;
if (!(target < 0 || target > 100000) && !(dist[target] != -1)) {
dist[target] = dist[cur];
q.push(target);
}
for (int new_: {
cur - 1,
cur + 1
}) {
if (new_ < 0 || new_ > 100000) continue;
if (dist[new_] != -1) continue;
dist[new_] = dist[cur] + 1;
q.push(new_);
}
}
cout << dist[k];
}
알고리즘 분류를 보니 0-1 BFS라는 게 있었다. 그냥 아무 생각 없이 풀었는데 이것도 따로 분류가 있구나.
나도 멋있게 cpp로 풀고싶어