C++
$\color{gold}{— > 蓝桥杯辅导课题解}$
思路:
dp
$图解分析:$
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 110;
int T;
int 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];
memset(f, 0, sizeof f);
for (int i = 1; i <= n; i ++)
for (int j = 1; j <= m; j ++)
f[i][j] = max(f[i - 1][j] + w[i][j], f[i][j - 1] + w[i][j]); // 状态转移
cout << f[n][m] << endl;
}
return 0;
}