解题思路:
动态规划。
注意事项:
走的路线从下到上只能走上面的或左上面。
参考代码:
n = int(input()) mapL = [list(map(int,input().split())) for _ in range(n)] dp = [] for i in range(n): dp.append([0]*(i+1)) dp[0][0] = mapL[0][0] for i in range(1,n): for j in range(0,len(mapL[i])): if j == 0: dp[i][j] = dp[i-1][j] + mapL[i][j] elif j == len(mapL[i]) - 1: dp[i][j] = dp[i-1][j-1] + mapL[i][j] else: dp[i][j] = max(dp[i-1][j],dp[i-1][j-1]) + mapL[i][j] print(max(dp[n-1]))
0.0分
3 人评分