-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.py
More file actions
42 lines (35 loc) · 1 KB
/
graph.py
File metadata and controls
42 lines (35 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#####메모리 초과.....######
# def HideandSeek(n,k):
# if n+1 == k or n-1 == k or 2*n == k:
# return
# else:
# HideandSeek(n+1,k)
# HideandSeek(n-1,k)
# HideandSeek(2*n,k)
# HideandSeek(5,17)
from collections import deque
def HideandSeek(n, k):
if n < 0 or n > 100000 or k < 0 or k > 100000:
return None
else:
queue = deque([[0, n]])
shortcut = 1000000
result = []
while queue: # 큐가 빌 때까지 반복
v = queue.popleft() # 원소 하나 뽑기
if v[0] > shortcut:
break
else:
if v[1] == k:
shortcut = v[0]
result.append(v)
else:
h = v[0]+1
queue.append([h, v[1]+1])
queue.append([h, v[1]-1])
queue.append([h, 2*v[1]])
print(result[0][0])
print(len(result))
print(result)
n, k = map(int, input().split())
HideandSeek(n, k)