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.

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

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.


In [1]:
import numpy as np

In [2]:
def roam(m, s, row, column):
    further = []
    for dr, dc in ((0, 1), (0, -1), (1, 0), (-1, 0)):
        r = row + dr
        c = column + dc
        if r < 0 or r >= len(m) or c < 0 or c >= len(m[0]):
            continue
        if (s[r, c] is None or
        s[row, column] + m[r, c] < s[r, c]):
            s[r, c] = s[row, column] + m[r, c]
            further.append((r, c))
    # print row, column, further
    for r, c in further:
        roam(m, s, r, c)

In [3]:
def foo(m):
    big = None
    # s = [[2 ** 64 - 1] * len(m[0])] * len(m)  # Slow. 
    s = [[big] * len(m[0])] * len(m)
    # s = [[2 ** 31 - 1] * len(m[0])] * len(m)  # Not slow.
    s = np.array(s)
    
    s[0, 0] = m[0, 0]
    roam(m, s, 0, 0)
    return s[-1,-1]

In [4]:
n = [[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]]
n = np.array(n)
print n

%timeit foo(n)
foo(n)


[[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]]
1000 loops, best of 3: 1.21 ms per loop
Out[4]:
2297

In [5]:
def roam(m, s, row, column):
    further = []
    for dr, dc in ((0, 1), (0, -1), (1, 0), (-1, 0)):
        r = row + dr
        c = column + dc
        if r < 0 or r >= len(m) or c < 0 or c >= len(m[0]):
            continue
        if (s[r, c] is None or
        s[row, column] + m[r, c] < s[r, c]):
            s[r, c] = s[row, column] + m[r, c]
            further.append((r, c))
    # print row, column, further
    return further

In [6]:
def foo(m):
    big = None
    # s = [[2 ** 64 - 1] * len(m[0])] * len(m)  # Slow. 
    s = [[big] * len(m[0])] * len(m)
    # s = [[2 ** 31 - 1] * len(m[0])] * len(m)  # Not slow.
    s = np.array(s)
    
    s[0, 0] = m[0, 0]
    further = [(0, 0)]
    while further:
        new_further = []
        for r, c in further:
            new_further.extend(roam(m, s, r, c))
        further = new_further
    return s[-1,-1]

In [7]:
n = [[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]]
n = np.array(n)
print n

%timeit foo(n)
foo(n)


[[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]]
1000 loops, best of 3: 732 us per loop
Out[7]:
2297

In [8]:
# http://projecteuler.net/project/matrix.txt
n = np.array([map(int, line.strip().split(',')) for line in open('matrix.txt').readlines()])
# print n

%timeit foo(n)
foo(n)


1 loops, best of 3: 509 ms per loop
Out[8]:
425185