#include <iostream>
#include <queue>
 
int main() 
{
    int A = 0;
    int B = 0;
    std::cin >> A >> B;
    std::queue<std::pair<long long,int>> que = {};
    que.push({ A,0 });
 
    while (!que.empty())
    {
        long long lNumber = que.front().first;
        int iCount = que.front().second;
        que.pop();
 
        if (lNumber == B)
        {
            std::cout << iCount + 1;
            return 0;
        }
 
        if (lNumber * 10 + 1 <= B)
            que.push({ lNumber * 10 + 1, iCount + 1 });
 
        if (lNumber * 2 <= B)
            que.push({ lNumber * 2, iCount + 1 });
    }
 
    std::cout << -1;
 
    return 0;
}
 

딱히 설명할 게 없어보이네요. que로 크기를 확인하고 가능하다다면 입력, 불가능하다면 입력하지 않습니다.

+ Recent posts