HDU 1559 最大子矩阵

最大子矩阵

Time Limit : 30000/10000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 3   Accepted Submission(s) : 1

Font: Times New Roman | Verdana | Georgia

Font Size:

Problem Description

给你一个m×n的整数矩阵,在上面找一个x×y的子矩阵,使子矩阵中所有元素的和最大。

Input

输入数据的第一行为一个正整数T,表示有T组测试数据。每一组测试数据的第一行为四个正整数m,n,x,y(0<m,n<1000 AND 0<x<=m AND 0<y<=n),表示给定的矩形有m行n列。接下来这个矩阵,有m行,每行有n个不大于1000的正整数。

Output

对于每组数据,输出一个整数,表示子矩阵的最大和。

Sample Input

1
4 5 2 2
3 361 649 676 588
992 762 156 993 169
662 34 638 89 543
525 165 254 809 280

Sample Output

2474

Author

lwg

Source

HDU 2006-12 Programming Contest
 
思路:DP
 
代码:

#include <iostream>

#include <cstdio>

#include <cstring>

#include <cstdlib>

using namespace std;

int map[1100][1100];

int temp[1100];

int t;

int n,m,x,y;

int main() {

        scanf("%d",&t);        

while(t --)        

{            

scanf("%d%d%d%d",&n,&m,&x,&y);          

   for(int i = 1;i <= n;i ++)   

             for(int j = 1;j <= m;j ++)

                  scanf("%d",&map[i][j]);

            int max = 0;

            for(int i = 1;i <= n - x + 1;i ++)

            {                

memset(temp,0,sizeof(temp));            

     for(int j = 1;j <= m;j ++)

{        

              temp[j] = temp[j - 1];       

             for(int k = i;k < i + x;k ++)   

                   temp[j] += map[k][j];     

                 }             

    for(int k = y;k <= m;k ++){      

               //printf("%d\n",temp[k] - temp[k - y]);    

                 if(temp[k] - temp[k - y] > max)               

         max = temp[k] - temp[k - y];}         

    }       

      printf("%d\n",max);   

      }     

    return 0;

}

 

posted on 2013-09-10 23:04  天使是一个善良的神  阅读(166)  评论(0编辑  收藏  举报

导航