AcWing 1015. 摘花生
原题链接
简单
作者:
冷丁Hacker
,
2020-09-12 10:25:58
,
所有人可见
,
阅读 610
简单的DP终于能自己想出来了
#include<bits/stdc++.h>
using namespace std;
const int N = 1010;
int n, m, q;
int a[N][N];
int f[N][N];
int main()
{
cin >> q;
while (q--) {
cin >> n >> m;
memset(a, 0, sizeof(0));
memset(f, 0, sizeof(0));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> a[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]) + a[i][j];
}
}
cout << f[n][m] << endl;
}
return 0;
}