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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值