문제 : https://www.acmicpc.net/problem/16953
나의 풀이
import sys
from collections import deque
input = sys.stdin.readline
def solution(a, b):
q = deque([(a, 1)])
while q:
n, cnt = q.popleft()
if n == b:
return cnt
if n * 2 <= b:
q.append((n * 2, cnt + 1))
if n * 10 + 1 <= b:
q.append((n * 10 + 1, cnt + 1))
return -1
a, b = map(int, input().split())
print(solution(a, b))
큐를 이용하여 풀 수 있는 문제이다. 가능한 연산을 수행한 값과 연산 횟수를 모두 큐에 저장하여 먼저 리턴되는 값을 답으로 연산의 최솟값을 구할 수 있다.
'코딩테스트 > 백준 (python)' 카테고리의 다른 글
백준 / 11659번 / 구간 합 구하기 4 / python 파이썬 (0) | 2025.01.02 |
---|---|
백준 / 1987번 / 알파벳 / python 파이썬 (0) | 2024.12.02 |
백준 / 1927번 / 최소 힙 / python 파이썬 (0) | 2024.11.22 |
백준 / 1766번 / 문제집 / python 파이썬 (0) | 2024.11.17 |
백준 / 12865번 / 평범한 배낭 / python 파이썬 (1) | 2024.11.12 |