The code below is for calculating the minimal traversing distance from the top left point of the matrix to the bottom right point of the matrix.
Here is the core functionality. Please note that y is a global variable keeping track of the memoization.
def findLeastPath(matrix, row=0, column=0, path=[], sum_=0):
row_max = matrix.shape[0]
column_max = matrix.shape[1]
if column == column_max - 1 and row == row_max - 1:
y[row][column] = matrix[row][column]
return matrix[row][column] + sum_
else:
current_cost = matrix[row][column]
path.append((row, column, current_cost))
if column == column_max - 1:
if y[row+1][column] != 0:
y[row][column] = current_cost + y[row+1][column]
return current_cost + y[row+1][column]
else:
down = findLeastPath(matrix, row + 1, column, path, sum_ + current_cost)
y[row][column] = current_cost + y[row+1][column]
return down
elif row == row_max - 1:
if y[row][column+1] != 0:
y[row][column] = current_cost + y[row][column + 1]
return current_cost + y[row][column + 1]
else:
right = findLeastPath(matrix, row, column + 1, path, sum_ + current_cost)
y[row][column] = current_cost + y[row][column + 1]
return right
else:
if y[row][column + 1] != 0:
right = y[row][column + 1] + current_cost
else:
findLeastPath(matrix, row, column + 1, path, sum_ + current_cost)
right = y[row][column + 1] + current_cost
if y[row + 1][column] != 0:
down = y[row + 1][column] + current_cost
else:
findLeastPath(matrix, row + 1, column, path, sum_ + current_cost)
down = y[row+1][column] + current_cost
if y[row + 1][column + 1] != 0:
diagonal = y[row + 1][column + 1] + current_cost
else:
findLeastPath(matrix, row + 1, column + 1, path, sum_ + current_cost)
diagonal = y[row+1][column+1] + current_cost
min_val = min(right, down, diagonal)
y[row][column] = min_val
return min_val