题解:
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; }