题目:
1015. 摘花生 - AcWing题库
思路:dp
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
const int N = 110;
typedef long long ll;
int T, r, c;
int num[N][N];
ll dp[N][N];//dp表示从(1,1)到达(i,j)的最大值
int i, j;
int main()
{
cin >> T;
while (T--) {
cin >> r >> c;
//输入
for (int i = 1; i <= r; i++)
for (int j = 1; j <= c; j++)
scanf("%d", &num[i][j]);
//dp从(1,1)到达(i,j)的最大值
//划分(到达(i,j)的前一步是向下还是向右)
for (i = 1; i <= r; i++)
for (j = 1; j <= c; j++)
dp[i][j] = max(dp[i - 1][j] + num[i][j], dp[i][j - 1] + num[i][j]);
cout << dp[r][c] << endl;
dp[N][N] = { 0 };//归0
}
}