Sum Of SubRectangular Parallelepiped

Sum Of SubRectangular Parallelepiped

Totalsubmit: 508   Accepted: 148

Description

There is a rectangular parallelepiped with a integer in each cell.Can you caculate the maximum sum of sub rectangular parallelepipeds?

Input

Input contains multiple test cases. The first line is the test number T, followed by cases. Each case begin with three integer L,M,N presenting the rectangular parallelepiped you will get. Then L matrixes each with M rows and N column follow.

Output

For each case print a line with the maximum sum of sub rectangular parallelepipeds.If all the integers are negtive,print 0.

Sample Input


1
2 2 2
7 -4
3 7

-9 -6
4 7

Sample Output


21

 I'm so guilty to AC this problem which had been 'A'd by 老蒋 yesterday evening...

三维dp

用i,j,k,p,q分别记录起始列,起始行,结束列,结束行,当前层;共5重循环。

#include<iostream> using namespace std; #define N 102 int a[N][N][N]; int sum[N][N][N]; int i,j,k; int l,m,n; int t; int main() { int i,j,k,p,q; scanf("%d",&t); while (t--) { scanf("%d%d%d",&l,&m,&n); for (i=0;i<l;i++) for (j=0;j<m;j++) for (k=0;k<n;k++) scanf("%d",&a[i][j][k]); memset(sum,0,sizeof(sum)); for (i=0;i<l;i++) { for (j=0;j<m;j++) for (k=0;k<n;k++) { if (j==0&&k==0) sum[i][j][k]=a[i][j][k]; else if (j==0) sum[i][j][k]=sum[i][j][k-1]+a[i][j][k]; else if (k==0) sum[i][j][k]=sum[i][j-1][k]+a[i][j][k]; else sum[i][j][k]=sum[i][j-1][k]+sum[i][j][k-1]-sum[i][j-1][k-1]+a[i][j][k]; } } int maxx=0; for (i=0;i<m;i++) { for (j=0;j<n;j++) for (k=i;k<m;k++) for (p=j;p<n;p++) { int s=0; for(q=0;q<l;q++) { int a; if(i==0&&j==0) a=sum[q][k][p]; else if(i==0) a=sum[q][k][p]-sum[q][k][j-1]; else if(j==0) a=sum[q][k][p]-sum[q][i-1][p]; else a=sum[q][k][p]-sum[q][i-1][p]-sum[q][k][j-1]+sum[q][i-1][j-1]; s+=a; if(s<0) s=0; maxx=maxx>s?maxx:s; } } } printf("%d/n",maxx); } return 0; }

你可能感兴趣的:(Sum Of SubRectangular Parallelepiped)