python算法之三角形问题

"""
经典的数字三角形问题(简单易懂,经典动态规划)
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5

D(r,j):


"""

l = [[7],[3,8], [8,1,0], [2,7,4,4],[4,5,2,6,5]]
n = 5

def get_max_sum(row, col):
if n == row:
print("%d \n" % l[row-1][col-1])
return l[row-1][col-1]
else:
sum_row_1_col = get_max_sum(row+1, col)
sum_row_1_col_1 = get_max_sum(row+1, col+1)
if sum_row_1_col > sum_row_1_col_1:

return sum_row_1_col + l[row-1][col-1]
else:
return sum_row_1_col_1 + l[row-1][col-1]

print(get_max_sum(1,1))

你可能感兴趣的:(python算法之三角形问题)