해야만 한다
[Python] 백준 16953. A → B 본문
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 |