#include <iostream>
#include <queue>

using namespace std;

// 최대 범위 설정 (0 ~ 100,000)
int visited[100001];
int N, K;

void bfs(int start) {
    queue<int> q;
    q.push(start);
    visited[start] = 1; // 시작 위치를 1로 표시 (0초를 의미)

    while (!q.empty()) {
        int x = q.front();
        q.pop();

        // 동생 위치에 도달하면 시간 출력 후 종료
        if (x == K) {
            cout << visited[x] - 1 << endl;
            return;
        }

        // 1. 뒤로 걷기 (x - 1)
        if (x - 1 >= 0 && visited[x - 1] == 0) {
            visited[x - 1] = visited[x] + 1;
            q.push(x - 1);
        }

        // 2. 앞으로 걷기 (x + 1)
        if (x + 1 <= 100000 && visited[x + 1] == 0) {
            visited[x + 1] = visited[x] + 1;
            q.push(x + 1);
        }

        // 3. 순간이동 (x * 2)
        if (x * 2 <= 100000 && visited[x * 2] == 0) {
            visited[x * 2] = visited[x] + 1;
            q.push(x * 2);
        }
    }
}

int main() {

    cin >> N >> K;

    // 수빈이와 동생의 위치가 같을 경우 0초 출력
    if (N == K) {
        cout << 0 << endl;
        return 0;
    }

    bfs(N);

    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: