Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

해야만 한다

[Python] 백준 16953. A → B 본문

Python/Algorithm

[Python] 백준 16953. A → B

쥬링999 2023. 10. 18. 12:35

 

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

 

 

풀이

bfs를 활용하여 풀었다.

 

코드

from collections import deque
def bfs():
    q = deque()
    q.append((a,0))
    while q:
        v, cnt = q.popleft()
        if v == b:
            return cnt+1
        if v*2 <= b:
            q.append((v*2,cnt+1))
        
        if v*10+1 <= b:
            q.append((v*10+1,cnt+1))
    return -1

a, b = map(int, input().split())
print(bfs())

 

'Python > Algorithm' 카테고리의 다른 글

[Python] 백준 1715. 카드 정렬하기  (0) 2023.10.18
[Python] 백준 1103. 게임  (0) 2023.10.18
[Python] 백준 1238. 파티  (0) 2023.10.18
[Python] 백준 1992. 쿼드트리  (0) 2023.10.14
[Python] 백준 14501. 퇴사  (0) 2023.10.14