-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdigging-a-canal.py
More file actions
136 lines (119 loc) · 4.14 KB
/
digging-a-canal.py
File metadata and controls
136 lines (119 loc) · 4.14 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import copy
def checkio(matrix):
counts, L = [], len(matrix[0])
for i in range(L):
for j in range(L):
coord = [0, i, len(matrix) - 1, j]
count = get_last_point(copy.deepcopy(matrix), *coord)
counts.append(count)
return min(counts)
def pop_min(labyrinth, queue):
min_q = [labyrinth[q[0]][q[1]] for q in queue]
min_point = min_q.index(min(min_q))
return queue.pop(min_point)
def get_last_point(labyrinth, xn, yn, xk, yk):
"""Initial Values"""
queue = []
queue.append([xn, yn])
N, M = len(labyrinth), len(labyrinth[0])
watched = [[False for jj in range(M)] for ii in range(N)]
watched[xn][yn] = True
"""Create path"""
while queue:
xn, yn = pop_min(labyrinth, queue)
for dx, dy in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
x, y = xn + dx, yn + dy
if (x >= 0) and (x < N) and \
(y >= 0) and (y < M) and \
not watched[x][y]:
queue.append([x, y])
watched[x][y] = True
labyrinth[x][y] = labyrinth[x][y] + labyrinth[xn][yn]
"""Return LastPoint"""
return labyrinth[xk][yk]
def test_last_point():
assert get_last_point([[0, 0, 1], [1, 2, 3], [1, 2, 3]], 0, 0, 2, 2) == 7, "3x3 diagonal"
"""
[
[0, 0, 1],
[1, 2, 4],
[2, 4, 7],
]
"""
assert get_last_point([[1, 1, 1], [1, 1, 1], [1, 1, 1]], 0, 0, 2, 2) == 5, "3x3 ones"
"""
[
[1, 2, 3],
[2, 3, 4],
[3, 4, 5],
]
"""
assert get_last_point([[10, 10], [10, 10]], 0, 0, 1, 1) == 30, "2x2 tens"
"""
[
[10, 20],
[20, 30],
]
"""
assert get_last_point([
[1, 1, 1, 1, 0, 1, 1],
[1, 1, 1, 1, 0, 0, 1],
[1, 1, 1, 1, 1, 0, 1],
[1, 1, 0, 1, 1, 0, 1],
[1, 1, 0, 1, 1, 1, 1],
[1, 0, 0, 1, 1, 1, 1],
[1, 0, 1, 1, 1, 1, 1]
], 0, 0, 6, 6) == 8, "big ex"
assert get_last_point([[1, 2, 3],
[1, 2, 3],
[1, 2, 3]], 0, 0, 2, 2) == 8, "3x3 ones"
def test_function():
assert checkio([[1, 1, 1, 1, 0, 1, 1],
[1, 1, 1, 1, 0, 0, 1],
[1, 1, 1, 1, 1, 0, 1],
[1, 10, 0, 1, 1, 0, 1],
[1, 1, 0, 1, 1, 1, 1],
[1, 0, 0, 1, 1, 1, 1],
[1, 0, 1, 1, 1, 1, 1]]) == 2, "1st example"
assert checkio([[0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1],
[1, 1, 0, 1, 0, 1, 1],
[1, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 0]]) == 3, "2nd example"
assert checkio([[1, 1, 1, 1, 1, 0, 1, 1],
[1, 0, 1, 1, 1, 0, 1, 1],
[1, 0, 1, 0, 1, 0, 1, 0],
[1, 0, 1, 1, 1, 0, 1, 1],
[0, 0, 1, 1, 0, 0, 0, 0],
[1, 0, 1, 1, 1, 1, 1, 1],
[1, 0, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 0, 1, 1, 1, 1]]) == 2, "3rd example"
assert checkio([[0, 1, 1, 1, 1, 1, 1, 1],
[0, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 0, 1],
[1, 1, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 1, 0, 1, 1, 0],
[0, 1, 1, 1, 0, 0, 0, 0]]) == 0, "horizontal range"
assert checkio([[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0]]) == 1, "vertical range"
def test_pop_min():
assert pop_min([[10, 10],
[10, 9]],
[[0, 0],
[1, 0],
[0, 1],
[1, 1]]) == [1, 1], "popmin"
if __name__ == '__main__':
test_last_point()
test_pop_min()
test_function()