본문 바로가기
Algorithm/BFS, DFS

[백준] 16953번 A → B

by 등촌동 꼬북이 2020. 9. 20.

숨바꼭질을 풀었다면 쉽게 풀 수 있는 문제

 

from queue import Queue

LIMIT = 1000000001

def BFS():
    flag = 0
    counter = 1
    que = Queue()
    que.put([A, counter])
    while not que.empty():
        tx, tc = que.get()
        counter = tc
        if tx == B:
            flag = 1
            break
        for nx in (tx * 2, int(str(tx)+'1')):
            if 0 < nx < LIMIT:
                que.put([nx, tc + 1])
    if flag:
        return counter
    return -1

A, B = map(int, input().split())

print(BFS())

'Algorithm > BFS, DFS' 카테고리의 다른 글

[백준] 10451번 순열 사이클  (0) 2020.09.21
[백준] 11724번 연결 요소의 개수  (0) 2020.09.20
[백준] 1967번 숨바꼭질  (0) 2020.09.15
[백준] 4963번 섬의 개수  (0) 2020.09.14
[백준] 7576번 토마토  (0) 2020.09.14

댓글