码迷,mamicode.com
首页 > 其他好文 > 详细

HDU1559

时间:2017-09-24 11:41:12      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:ane   style   auth   ted   bsp   enter   mis   key   bottom   

最大子矩阵

Time Limit: 30000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5199    Accepted Submission(s): 2747


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

 
 1 //2017-09-24
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <iostream>
 5 #include <algorithm>
 6 
 7 using namespace std;
 8 
 9 const int N = 1010;
10 
11 int a[N][N], dp[N][N], n, m, x, y;
12 
13 int main()
14 {
15     int T;
16     scanf("%d", &T);
17     while(T--){
18         scanf("%d%d%d%d", &n, &m, &x, &y);
19         for(int i = 0; i < n; i++)
20           for(int j = 0; j < m; j++){
21               scanf("%d", &a[i][j]);
22               dp[i][j] = a[i][j];
23           }
24         for(int i = 1; i < n; i++)
25           for(int j = 0; j < m; j++)
26             dp[i][j] += dp[i-1][j];
27         for(int j = 1; j < m; j++)
28           for(int i = 0; i < n; i++)
29             dp[i][j] += dp[i][j-1];
30         int ans = 0;
31         for(int i = x-1; i < n; i++)
32           for(int j = y-1; j < m; j++){
33               int a = i-x+1;
34               int b = j-y+1;
35               int tmp = dp[i][j];
36               if(a-1>=0)tmp -= dp[a-1][j];
37               if(b-1>=0)tmp -= dp[i][b-1];
38               if(a-1>=0 && b-1>=0)tmp += dp[a-1][b-1];
39               ans = max(ans, tmp);
40           }
41         printf("%d\n", ans);
42     }
43 
44     return 0;
45 }

 

HDU1559

标签:ane   style   auth   ted   bsp   enter   mis   key   bottom   

原文地址:http://www.cnblogs.com/Penn000/p/7586591.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!