香菇蓝瘦,我要开始怀疑人生了!!
同样是动态规划!!为什么我的程序通过不了全部测试???😭太匪夷所思了
R,C = list(map(int, input().split())) mat = [] dp = [[0]*C for _ in range(R)] for x in range(R): mat.append(list(map(int, input().split()))) if R==1 and C==1: print(mat[0][0]) exit() for j in range(C): for i in range(R): if i == 0 and j == 0: dp[i][j] = mat[i][j] elif i == 0 and j != 0: dp[i][j] = max(dp[i][j-1],dp[i+1][j-1]) + mat[i][j] elif i != 0 and j == 0: dp[i][j] = 0 elif i == R-1 and j != 0: dp[i][j] = max(dp[i-1][j-1],dp[i][j-1]) + mat[i][j] else: dp[i][j] = max(dp[i-1][j-1],dp[i][j-1],dp[i+1][j-1]) + mat[i][j] print(dp[R-1][C-1])