[알고리즘/백준] 14562번 : 태권왕(python)

유현민·2022년 3월 3일
0

알고리즘

목록 보기
28/253

간단하게 bfs로 풀면 된다. 두가지 경우를 다 큐에 넣고 답이 나올때 까지 돌리면 된다.

from collections import deque


def solution(S, T):
    C = 0
    q = deque()
    q.append([S, T, C])  # 처음 큐

    while q:
        s, t, c = q.popleft()
        if s < t:
            q.append([s * 2, t + 3, c + 1])
            q.append([s + 1, t, c + 1])
        elif s == t:
            print(c)
            break


if __name__ == "__main__":
    for _ in range(int(input())):
        S, T = map(int, input().split())
        solution(S, T)
profile
smilegate megaport infra

0개의 댓글