DP水题。。
#include<cstdio> #include<cstring> int t,i,j,n,m,s,a[105][105],f[105][105]; int max(int a,int b){return a>b?a:b;} int main(){ scanf("%d",&t); while(t--){ scanf("%d%d",&n,&m); for (i=1;i<=n;i++) for (j=1;j<=m;j++) scanf("%d",&a[i][j]); memset(f,0,sizeof(f)); for(i=1;i<=n;i++) for (j=1;j<=m;j++){ f[i][j]=max(max(f[i-1][j],f[i-1][j-1]),f[i-1][j+1])+a[i][j]; } s=0; for (i=1;i<=m;i++) if (s<f[n][i])s=f[n][i]; printf("%d\n",s); } }
时间: 2024-12-23 05:44:00