题解:

dp

f[i][j]=max(f[i-1][j+1]+a[i][j],max(f[i-1][j],f[i][j+1]));

代码:

#include<bits/stdc++.h>
using namespace std;
const int N=1005;
int T,n,m,a[N][N],f[N][N];
int main()
{
    scanf("%d",&T);
    while (T--)
     {
        scanf("%d%d",&n,&m);
        for (int i=1;i<=n;i++)
         for (int j=1;j<=m;j++)scanf("%d",&a[i][j]);
        for (int i=1;i<=n;i++)
         for (int j=m;j;j--)
          f[i][j]=max(f[i-1][j+1]+a[i][j],max(f[i-1][j],f[i][j+1]));
        printf("%d\n",f[n][1]);
     }
    return 0;
}

 

posted on 2018-03-14 21:10  宣毅鸣  阅读(135)  评论(0编辑  收藏  举报