Path sum: four ways

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.

$$ \begin{pmatrix} \color{red}{131} & 673 & \color{red}{234} & \color{red}{103} & \color{red}{18}\\ \color{red}{201} & \color{red}{96} & \color{red}{342} & 965 & \color{red}{150}\\ 630 & 803 & 746 & \color{red}{422} & \color{red}{111}\\ 537 & 699 & 497 & \color{red}{121} & 956\\ 805 & 732 & 524 & \color{red}{37} & \color{red}{331} \end{pmatrix} $$

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.


Idea

A typical Dijkstra problem.


In [1]:
from urllib.request import urlopen
from itertools import product
In [2]:
with urlopen('https://projecteuler.net/project/resources/p083_matrix.txt') as f:
    resp = f.read().decode('utf-8')
In [3]:
matrix = [list(map(int, line.split(','))) for line in resp.splitlines()]
In [4]:
len(matrix), len(matrix[0])
Out[4]:
(80, 80)
In [5]:
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]
]
In [6]:
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
In [7]:
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
In [8]:
def solve(matrix):
    return dijkstra(matrix)
In [9]:
solve(example_matrix)
Out[9]:
2297
In [10]:
solve(matrix)
Out[10]:
425185