NOTE: This problem is a significantly more challenging version of Problem 81.
In the 5 by 5 matrix below, the minimal path sum from the top left to the bottom right, by moving left, right, up, and down, is indicated in bold red and is equal to 2297.
Find the minimal path sum, in matrix.txt (right click and "Save Link/Target As..."), a 31K text file containing a 80 by 80 matrix, from the top left to the bottom right by moving left, right, up, and down.
A typical Dijkstra problem.
from urllib.request import urlopen
from itertools import product
with urlopen('https://projecteuler.net/project/resources/p083_matrix.txt') as f:
resp = f.read().decode('utf-8')
matrix = [list(map(int, line.split(','))) for line in resp.splitlines()]
len(matrix), len(matrix[0])
example_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]
]
def get_adjacent_point(p, end_point):
r, c = p
h, w = end_point
points = [(r-1, c), (r+1, c), (r, c-1), (r, c+1)]
if r == 0:
points.remove((r-1, c))
if r == h:
points.remove((r+1, c))
if c == 0:
points.remove((r, c-1))
if c == w:
points.remove((r, c+1))
return points
def dijkstra(matrix):
end_point = len(matrix)-1, len(matrix[0])-1
assert end_point[0] == end_point[1]
distance = {}
previous = {}
points = list(product(range(end_point[0]+1), range(end_point[1]+1)))
for p in points:
distance[p] = float('inf')
previous[p] = None
distance[(0, 0)] = matrix[0][0]
s, q = set(), set(points)
while q:
u = min(q, key=lambda p: distance[p])
if u == end_point:
return distance[u]
q.remove(u)
s.add(u)
for p in get_adjacent_point(u, end_point):
if distance[p] > distance[u] + matrix[p[0]][p[1]]:
distance[p] = distance[u] + matrix[p[0]][p[1]]
previous[p] = u
def solve(matrix):
return dijkstra(matrix)
solve(example_matrix)
solve(matrix)