AcWing
1015. 摘花生
f[i][j]
:所有到(i, j)
的方案中,花生总数的最大值。
状态转移方程:f[i][j] = max(f[i - 1][j], f[i][j - 1]) + w[i][j]
.
#include <bits/stdc++.h>
using namespace std;
int t, r, c;
const int N = 110;
int w[N][N];
int f[N][N];
int main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> t;
while(t--){
cin >> r >> c;
for(int i = 1; i <= r; i++) for(int j = 1; j <= c; j++) cin >> w[i][j];
for(int i = 1; i <= r; i++) for(int j = 1; j <= c; j++)
f[i][j] = max(f[i - 1][j], f[i][j - 1]) + w[i][j];
cout << f[r][c] << endl;
}
return 0;
}