zxh2002 @ 2021-04-27 11:32:20
为什么最后一个点runtime error 小白求助
# 最大数字路径
def maxsum(i, j):
if (dp[i][j] != -1):
return dp[i][j]
if (i == r):
dp[i][j] = a[i][j]
else:
x = maxsum(i + 1, j)
y = maxsum(i + 1, j + 1)
dp[i][j] = max(x, y) + a[i][j]
return dp[i][j]
r = int(input())
a = [[] for i in range(r + 2)] # 创建二维数组的方法(此处为r+2行数组,防止数据溢出)
dp = [[] for i in range(r + 2)]
for i in range(1, r + 1):
a[i]=[int (value)for value in input().split()]
a[i].insert(0,0)
for i in range(1, r + 1):
for j in range(1, i + 1):
dp[i].append(-1) # 给dp数组初始化
for i in range(1,r+1):
dp[i].insert(0,-1)
print(maxsum(1, 1))
by ஐ喃旧ꦿ @ 2021-06-24 18:59:42
动态转移方程式:f[i][j]=max(f[i-1][j],f[i-1][j+1]) emmm
by ஐ喃旧ꦿ @ 2021-06-24 19:05:09
@王子诚1031 嘿嘿嘿(bushi 伪代码之王
by LaoXu666 @ 2021-08-08 12:16:34
@ஐ喃旧ꦿ 明明是Python代码!