We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 2563b8b commit 2b2bc74Copy full SHA for 2b2bc74
깊이or너비 우선 탐색/A_B.py
@@ -0,0 +1,35 @@
1
+# https://www.acmicpc.net/problem/16953
2
+
3
+"""
4
+bfs적용했지만 메모리 초과가 났던 문제
5
+방문을 처리하기 위해 목표 숫자의 최댓값 크기의 리스트를 선언했는데 그 크기가 너무 컸기 때문임
6
+그래서 방문처리를 하기위해
7
8
9
+from collections import deque,defaultdict
10
11
+a,b = map(int,input().split())
12
+visited = []
13
+cal_cnt = defaultdict(lambda: -1)
14
15
+def bfs(start):
16
+ q = deque()
17
+ q.append(start)
18
+ visited.append(start)
19
+ cal_cnt[start] = 1
20
21
+ while q:
22
+ value = q.popleft()
23
24
+ num = 2*value
25
+ if num <= b and num not in visited:
26
+ q.append(num)
27
+ cal_cnt[num] = cal_cnt[value] + 1
28
29
+ num = 10*value + 1
30
31
32
33
34
+bfs(a)
35
+print(cal_cnt[b])
0 commit comments