Skip to content

Commit

Permalink
A->B 문제 파일이름 수정(윈도우 호환성 문제)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeVicTech committed May 20, 2023
1 parent 2563b8b commit 2b2bc74
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions 깊이or너비 우선 탐색/A_B.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# https://www.acmicpc.net/problem/16953

"""
bfs적용했지만 메모리 초과가 났던 문제
방문을 처리하기 위해 목표 숫자의 최댓값 크기의 리스트를 선언했는데 그 크기가 너무 컸기 때문임
그래서 방문처리를 하기위해
"""

from collections import deque,defaultdict

a,b = map(int,input().split())
visited = []
cal_cnt = defaultdict(lambda: -1)

def bfs(start):
q = deque()
q.append(start)
visited.append(start)
cal_cnt[start] = 1

while q:
value = q.popleft()

num = 2*value
if num <= b and num not in visited:
q.append(num)
cal_cnt[num] = cal_cnt[value] + 1

num = 10*value + 1
if num <= b and num not in visited:
q.append(num)
cal_cnt[num] = cal_cnt[value] + 1

bfs(a)
print(cal_cnt[b])

0 comments on commit 2b2bc74

Please sign in to comment.