Sum Of SubRectangular Parallelepiped 三维最大子段和

Sum Of SubRectangular Parallelepiped

TimeLimit: 2 Second   MemoryLimit: 32 Megabyte

Totalsubmit: 468   Accepted: 132  

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

 

 

 

#include<iostream>
#include<cstring>
using namespace std;

int a[100][100][100];
int summ[100][100][100];
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(summ,0,sizeof(summ));
  for (i=0;i<l;i++)
   for (j=0;j<m;j++)
       for (k=0;k<n;k++)
       {
     if (j==0&&k==0)
     {
      summ[i][j][k]=a[i][j][k];
      continue;
     }
     if (j==0)
     {
      summ[i][j][k]=summ[i][j][k-1]+a[i][j][k];
      continue;
     }
     if (k==0)
     {
      summ[i][j][k]=summ[i][j-1][k]+a[i][j][k];
      continue;
     }
     summ[i][j][k]=summ[i][j-1][k]+summ[i][j][k-1]-summ[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=summ[q][k][p];
       else if (i==0)
        a=summ[q][k][p]-summ[q][k][j-1];
       else if (j==0)
        a=summ[q][k][p]-summ[q][i-1][p];
       else
        a=summ[q][k][p]-summ[q][i-1][p]-summ[q][k][j-1]+summ[q][i-1][j-1];
       s+=a;
       if (s<0) s=0;
       else if(s>maxx) maxx=s;
      }
     }
  printf("%d/n",maxx);
 }
 return 0;
}

 

你可能感兴趣的:(Sum Of SubRectangular Parallelepiped 三维最大子段和)