题面
题解(数字三角形模型)
代码
#include<bits/stdc++.h>
using namespace std;
const int N = 110;
int t, n, m;
int w[N][N];
int f[N][N];
int main() {
cin >> t;
while (t--) {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> w[i][j];
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
f[i][j] = max(f[i - 1][j], f[i][j - 1]) + w[i][j];
}
}
cout << f[n][m] << endl;
}
return 0;
}