Skip to content

Commit 7ca4ff8

Browse files
committed
[BOJ] #16953. A->B / 실버2 / 40분 / 힌트,성공
1 parent 46d7622 commit 7ca4ff8

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import sys
2+
from collections import deque
3+
input = sys.stdin.readline
4+
5+
# 입력 받기
6+
A, B = map(int, input().split())
7+
8+
# 큐 초기화
9+
queue = deque([A])
10+
cnt = 1 # 연산 횟수
11+
12+
while queue:
13+
# 현재 큐에서 모든 값을 탐색
14+
for _ in range(len(queue)):
15+
current = queue.popleft()
16+
17+
# 현재 값이 B와 같으면 결과 출력 후 종료
18+
if current == B:
19+
print(cnt)
20+
exit()
21+
22+
# 다음 값을 큐에 추가 (B를 초과하지 않는 경우만)
23+
if current * 2 <= B:
24+
queue.append(current * 2)
25+
if int(str(current)+"1") <= B:
26+
queue.append(int(str(current)+"1"))
27+
28+
# 연산 횟수 증가
29+
cnt += 1
30+
31+
# 큐가 비었다면 -1 출력
32+
print(-1)

0 commit comments

Comments
 (0)