$$\color{Red}{摘花生-三种方法-详细题解}$$
这里附带打个广告——————我做的所有的题解
包括基础提高以及一些零散刷的各种各样的题
数字三角形模型
java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class Main {
static int N = 110, n, m, T;
static int[][] a = new int[N][N];
static int[][] f = new int[N][N];
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException {
T = Integer.parseInt(br.readLine());
while (T-- > 0) {
String[] str1 = br.readLine().split(" ");
n = Integer.parseInt(str1[0]);
m = Integer.parseInt(str1[1]);
for (int i = 1; i <= n; i++) {
String[] str2 = br.readLine().split(" ");
for (int j = 1; j <= m; j++)
a[i][j] = Integer.parseInt(str2[j - 1]);
}
for (int i = 0; i <= n; i++) Arrays.fill(f[i], 0);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
f[i][j] = Math.max(f[i - 1][j], f[i][j - 1]) + a[i][j];
System.out.println(f[n][m]);
}
}
}
python
N = 110
g = [[0]*N for i in range(N)]
f = [[0]*N for i in range(N)]
t = int(input())
for i in range(t):
n, m = map(int, input().split())
for i in range(1, n+1):
g[i][1:m+1] = map(int, input().split())
for i in range(1, n+1):
for j in range(1, m+1):
f[i][j] = max(f[i-1][j], f[i][j-1]) + g[i][j]
print(f[n][m])