BOJ - 16953 - A → B

Updated:

import sys
from collections import deque

def BFS(A, B):
    Q = deque()
    Q.append((A, 1))

    while Q:
        number, move = Q.popleft()

        if number == B:
            return move

        if number * 2 <= B:
            Q.append((number * 2, move + 1))
        if (number * 10) + 1 <= B:
            Q.append(((number * 10) + 1, move + 1))

    return -1
    
def solution():
    A, B = map(int, sys.stdin.readline().split())
    print(BFS(A, B))
solution()

https://www.acmicpc.net/problem/16953

Categories:

Updated:

Leave a comment