【发布时间】:2020-04-21 21:05:30
【问题描述】:
例如,给定一个成本矩阵,我不仅需要知道从 [0,0] 到 [N-1, N-1] 的最大总成本,还需要知道我使用这条最优路径所做的移动.同时,水平移动的优先级高于垂直移动。
| 1 | 2 | 3 |
| 2 |-8 | 4 |
| 3 | 4 | 6 |
我知道这可以通过动态编程来解决。但是,如何记录需要进行的运动?
在这种情况下,两条路径:(right, right, down, down) 的总成本与 (down, down, right, right) 完全相同,最优路径仅为 (right, right, down, down) )。
为了说清楚,我的问题是,如何找出最优路径的所有运动?
def optimal(mat):
dim = len(mat)
cost = [[0]*dim for i in range(dim)]
path = []
if mat[0][1]>=mat[1][0]:
path.append('r')
else:
path.append('d')
cost[0][0] = mat[0][0]
for i in range(1,dim):
cost[i][0] = mat[i][0] + cost[i - 1][0]
for j in range(1,dim):
cost[0][j] = mat[0][j] + cost[0][j - 1]
for i in range(1, dim):
for j in range(1, dim):
if cost[i-1][j] >= cost[i][j-1]:
cost[i][j] = cost[i-1][j] + mat[i][j]
else:
cost[i][j] = cost[i][j-1] + mat[i][j]
print(cost[dim-1][dim-1])
print(path)
【问题讨论】:
-
“水平移动优先于垂直移动”是什么意思?如果您有两条成本相同的路径,您会选择首先使用水平移动的路径吗?你是否最大化水平移动的次数?
-
你在标题中的问题和在帖子中的问题是不同的。另外,从 1 到 6 的最大成本是 14?
-
@André Cascais 例如,如果两条路径 [down, right, right, down] 和 [down, down, right, right] 给出相同的总成本,[down, right, right, down ] 将是我想要的那个。这不仅仅是第一步。
-
@Vicrobot for matrix 1, Path 1 -> 1 -> 2 -> 4 -> 6 将给出最大总成本,1+1+2+4+6 =14。
标签: python matrix dynamic-programming