-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path경주로 건설.py
61 lines (47 loc) · 1.79 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
54
55
56
57
58
59
60
61
from collections import deque
def solution(board):
def move_left(row, col):
if col == 0 or board[row][col-1] == 1:
return
return row, col-1
def move_right(row, col):
if col == len(board[0]) - 1 or board[row][col+1] == 1:
return
return row, col+1
def move_up(row, col):
if row == 0 or board[row-1][col] == 1:
return
return row-1, col
def move_down(row, col):
if row == len(board) - 1 or board[row+1][col] == 1:
return
return row+1, col
visited = [[2_100_000_000]*len(board[0]) for _ in range(len(board))]
row, col, cost = 0, 0, 0
queue = deque([[row, col, cost, 'first_pass']])
while queue:
row, col, cost, before = queue.popleft()
if visited[row][col] < cost:
continue
visited[row][col] = cost
if move_left(row, col):
if before == 'first_pass' or before == 'left':
queue.append([row, col-1, cost+100, 'left'])
else:
queue.append([row, col-1, cost+600, 'left'])
if move_right(row, col):
if before == 'first_pass' or before == 'right':
queue.append([row, col+1, cost+100, 'right'])
else:
queue.append([row, col+1, cost+600, 'right'])
if move_up(row, col):
if before == 'first_pass' or before == 'up':
queue.append([row-1, col, cost+100, 'up'])
else:
queue.append([row-1, col, cost+600, 'up'])
if move_down(row, col):
if before == 'first_pass' or before == 'down':
queue.append([row+1, col, cost+100, 'down'])
else:
queue.append([row+1, col, cost+600, 'down'])
return visited[-1][-1]