3997: [TJOI2015]组合数学
Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 806 Solved: 589
[ Submit][ Status][ Discuss]
Description
给出一个网格图,其中某些格子有财宝,每次从左上角出发,只能向下或右走。问至少走多少次才能将财宝捡完。此对此问题变形,假设每个格子中有好多财宝,而每一次经过一个格子至多只能捡走一块财宝,至少走多少次才能把财宝全部捡完。
Input
第一行为正整数T,代表数据组数。
每组数据第一行为正整数N,M代表网格图有N行M列,接下来N行每行M个非负整数,表示此格子中财宝数量,0代表没有
Output
输出一个整数,表示至少要走多少次。
Sample Input
1
3 3
0 1 5
5 0 0
1 0 0
3 3
0 1 5
5 0 0
1 0 0
Sample Output
10
HINT
N<=1000,M<=1000.每个格子中财宝数不超过10^6
Source
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn = 1010;
typedef long long LL;
int n,m,T,A[maxn][maxn];
LL f[maxn][maxn];
int getint()
{
char ch = getchar(); int ret = 0;
while (ch < '0' || '9' < ch) ch = getchar();
while ('0' <= ch && ch <= '9')
ret = ret*10 + ch - '0',ch = getchar();
return ret;
}
int main()
{
#ifdef DMC
freopen("DMC.txt","r",stdin);
#endif
T = getint();
while (T--)
{
n = getint(); m = getint();
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
A[i][j] = getint();
for (int i = 1; i <= n; i++) f[i][m + 1] = 0;
for (int i = 1; i <= n; i++)
for (int j = m; j; j--)
{
f[i][j] = max(f[i-1][j],f[i][j+1]);
f[i][j] = max(f[i][j],f[i-1][j+1] + 1LL * A[i][j]);
}
cout << f[n][1] << endl;
}
return 0;
}