-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheuler083.py
74 lines (68 loc) · 2.4 KB
/
euler083.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
62
63
64
65
66
67
68
69
70
71
72
73
from typing import List
with open("p082_matrix.txt") as infile:
matrix = infile.readlines()
matrix = [
[
int(n.strip())
for n in line.split(",")
]
for line in matrix
]
def pretty_print(matrix: List[List[int]]) -> None:
for line in matrix:
print(line)
def find_min_sum_path(matrix: List[List[int]]) -> int:
best_path = 1000000
row_max = len(matrix) - 1
col_max = len(matrix[0]) - 1
visited = [
[
False for _ in line
]
for line in matrix
]
best_sum = [
[
float("Inf") for _ in line
]
for line in matrix
]
to_check = [(0, 0)]
visited[0][0] = True
best_sum[0][0] = matrix[0][0]
while len(to_check) > 0:
row, col = to_check.pop(0)
# Check on the left
if col > 0:
if visited[row][col - 1] is False or best_sum[row][col - 1] > best_sum[row][col] + matrix[row][col - 1]:
best_sum[row][col - 1] = best_sum[row][col] + matrix[row][col - 1]
visited[row][col - 1] = True
to_check.append((row, col - 1))
# Check on top
if row > 0:
if visited[row - 1][col] is False or best_sum[row - 1][col] > best_sum[row][col] + matrix[row - 1][col]:
best_sum[row - 1][col] = best_sum[row][col] + matrix[row - 1][col]
visited[row - 1][col] = True
to_check.append((row - 1, col))
#Check on the right
if col < col_max:
if visited[row][col + 1] is False or best_sum[row][col + 1] > best_sum[row][col] + matrix[row][col + 1]:
best_sum[row][col + 1] = best_sum[row][col] + matrix[row][col + 1]
visited[row][col + 1] = True
to_check.append((row, col + 1))
# Check on bottom
if row < row_max:
if visited[row + 1][col] is False or best_sum[row + 1][col] > best_sum[row][col] + matrix[row + 1][col]:
best_sum[row + 1][col] = best_sum[row][col] + matrix[row + 1][col]
visited[row + 1][col] = True
to_check.append((row + 1, col))
return best_sum[row_max][col_max]
test_matrix = [
[131, 673, 234, 103, 18],
[201, 96, 342, 965, 150],
[630, 803, 746, 422, 111],
[537, 699, 497, 121, 956],
[805, 732, 524, 37, 331]
]
assert find_min_sum_path(test_matrix) == 2297
print(find_min_sum_path(matrix))