# 尝试确定最大成本路径，但仅适用于大小为2和3的矩阵

[0,1,2,0]

[3,4,5,0]

[4,5,6,0]

[5.6.8.0]

``````    cost = max(maxCostPathBottomLeft(building), maxCostPathBottomRight(building), maxCostPathTopLeft(building),
maxCostPathTopRight(building))
print("Cost to reach : ", cost)

def maxCostPaths(matrix):
maxCostPath = [[0 for x in range(len(matrix))] for y in range(len(matrix))]
print(len(matrix))
for i in range(0, int(len(matrix)/2) + 2):
aboveCost = maxCostPath[i - 1][0]
maxCostPath[i][0] = aboveCost + matrix[i][0]

for j in range(0, int(len(matrix)/2 + 2)):
leftCost = maxCostPath[0][j - 1]
maxCostPath[0][j] = leftCost + matrix[0][j]

for i in range(0, int(len(matrix)/2) + 2):
for j in range(0, int(len(matrix)/2) + 2):
leftCost = maxCostPath[i - 1][j]
aboveCost = maxCostPath[i][j - 1]
maxCostPath[i][j] = matrix[i][j] + max(leftCost, aboveCost)
print(maxCostPath)
print(maxCostPath[len(matrix) - 1][len(matrix) - 1])
return maxCostPath[len(matrix) - 1][len(matrix) - 1]

def maxCostPathBottomRight(matrix):
maxCostPath = [[0 for x in range(matrix.size + 1)] for y in range(matrix.size + 1)]
center = int(matrix.size)
b = 0
for a in range(center, matrix.size * 2 + 1):
d = 0
for c in range(center, matrix.size * 2 + 1):
maxCostPath[b][d] = matrix.rooms[a][c].food
d += 1
b += 1
return maxCostPaths(maxCostPath)

def maxCostPathBottomLeft(matrix):
maxCostPath = [[0 for x in range(matrix.size + 1)] for y in range(matrix.size + 1)]
center = int(matrix.size)
b = 0
for a in range(center, matrix.size * 2 + 1):
d = 0
for c in range(0, matrix.size + 1):
maxCostPath[b][d] = matrix.rooms[a][c].food
d += 1
b += 1
return maxCostPaths(maxCostPath)

def maxCostPathTopRight(matrix):
maxCostPath = [[0 for x in range(matrix.size + 1)] for y in range(matrix.size + 1)]
center = int(matrix.size)
b = 0
for a in range(center, matrix.size * 2 + 1):
d = 0
for c in range(0, matrix.size + 1):
maxCostPath[b][d] = matrix.rooms[a][c].food
d += 1
b += 1
return maxCostPaths(maxCostPath)

def maxCostPathTopLeft(matrix):
maxCostPath = [[0 for x in range(matrix.size + 1)] for y in range(matrix.size + 1)]
center = int(matrix.size)
b = 0
for a in range(0, matrix.size + 1):
d = 0
for c in range(0, matrix.size + 1):
maxCostPath[b][d] = matrix.rooms[a][c].food
d += 1
b += 1
return maxCostPaths(maxCostPath)
``````