-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path길찾기게임.py
53 lines (41 loc) · 1.31 KB
/
길찾기게임.py
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
43
44
45
46
47
48
49
50
51
52
53
def preorder(arr_y, arr_x, answer):
node = arr_y[0]
idx = arr_x.index(node)
arr_y1 = []
arr_y2 = []
for i in range(1, len(arr_y)):
if node[0] > arr_y[i][0]:
arr_y1.append(arr_y[i])
else:
arr_y2.append(arr_y[i])
answer.append(node[2])
if len(arr_y1) > 0:
preorder(arr_y1, arr_x[:idx], answer)
if len(arr_y2) > 0:
preorder(arr_y2, arr_x[idx + 1:], answer)
def postorder(arr_y, arr_x, answer):
node = arr_y[0]
idx = arr_x.index(node)
arr_y1 = []
arr_y2 = []
for i in range(1, len(arr_y)):
if node[0] > arr_y[i][0]:
arr_y1.append(arr_y[i])
else:
arr_y2.append(arr_y[i])
if len(arr_y1) > 0:
postorder(arr_y1, arr_x[:idx], answer)
if len(arr_y2) > 0:
postorder(arr_y2, arr_x[idx + 1:], answer)
answer.append(node[2])
def solution(nodeinfo):
preanswer = []
postanswer = []
for i in range(len(nodeinfo)):
nodeinfo[i].append(i+1)
arr_y = sorted(nodeinfo, key = lambda x : (-x[1], x[0]))
arr_x = sorted(nodeinfo)
preorder(arr_y, arr_x, preanswer)
postorder(arr_y, arr_x, postanswer)
return [preanswer, postanswer]
print(solution([[5,3],[11,5],[13,3],[3,5],[6,1],[1,3],[8,6],[7,2],[2,2]]))