volver

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.

(131673234103182019634296515063080374642211153769949712195680573252437331)

Find the minimal path sum from the top left to the bottom right by moving left, right, up, and down in matrix.txt (right click and "Save Link/Target As..."), a 31K text file containing an 80 by 80 matrix.

Solución

Igual que el problema 82, implementamos Dijkstra para calcular el camino optimo, considerando la matriz un grafo dirigido.

En este caso, en la función de calcular las distancias añadimos la cuarta dirección. Luego empezamos en la posición (0,0) y terminamos en (n1,n1) como cuando hacemos DTW.

def distances(cost_matrix, curr_row=0, curr_col=0):
    distances = np.full_like(cost_matrix, np.inf, dtype=np.float64)
    visited = np.full_like(cost_matrix, False, dtype=np.bool)

    distances[curr_row, curr_col]=cost_matrix[curr_row, curr_col]
    while np.max(distances)==np.inf:
        # buscamos el nodo con el menor coste, en la primera iteracion es el de la casilla inicial
        row, col = np.unravel_index(np.argmin(np.where(visited, np.inf, distances)), visited.shape)

        # accesible edges
        if row>0:
            if distances[row, col] + cost_matrix[row-1, col] < distances[row-1, col]:
                distances[row-1, col] = distances[row, col] + cost_matrix[row-1, col]
        if col>0:
            if distances[row, col] + cost_matrix[row, col-1] < distances[row, col-1]:
                distances[row, col-1] = distances[row, col] + cost_matrix[row, col-1]
        if row<visited.shape[0]-1:
            if distances[row, col] + cost_matrix[row+1, col] < distances[row+1, col]:
                distances[row+1, col] = distances[row, col] + cost_matrix[row+1, col]
        if col<visited.shape[1]-1:
            if distances[row, col] + cost_matrix[row, col+1] < distances[row, col+1]:
                distances[row, col+1] = distances[row, col] + cost_matrix[row, col+1]
            
        visited[row, col] = True

    return distances