import numpy as np
老师 , 那个回溯法的代码好像不太对 , 我用 python 写了一个
import sys
minDist = sys.maxsize
n = 4 # 这是个 4*4 的矩阵 .
w = np.array([[0,3,5,9],[2,1,3,4],[5,2,6,7],[6,8,4,3]])
# dist = np.zeros((4,4)) # 定义 dist(i, j) 为到达点 (i,j) 的路径长度
# dist[i, j] = w[i,j] + min(dist[i-1, j] , dist[i, j-1])
def minDistBackTrace(i, j, dist, w, n):
global minDist
dist += w[i][j]
if i==n -1 and j == n-1 :
if dist < minDist: minDist = dist
return
if i < n-1:
minDistBackTrace(i + 1, j, dist, w, n)
if j < n-1:
minDistBackTrace(i , j + 1, dist, w, n)
展开