hdu 1559 最大子矩阵 (简单dp)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1559

 1 #include <cstring>
 2 #include <cstdlib>
 3 #include <cstdio>
 4 #include <iostream>
 5 #include <algorithm>
 6 using namespace std;
 7 const int maxn = 1000+10;
 8 const int INF = 1<<28;
 9 int map[maxn][maxn];  //表示以[1,1]为左上角,以[i,j]为右下角的矩形的和
10 
11 int main()
12 {
13     int t, i, j, m, n, x, y;
14     int s1, sc, Max, ans;
15     scanf("%d", &t);
16     while(t--)
17     {
18         memset(map, 0, sizeof(map));
19         Max = -INF;
20         scanf("%d%d%d%d", &m, &n, &x, &y);
21         for(i = 1; i <= m; i++)
22         {
23             s1 = 0;
24             for(j = 1; j <= n; j++)
25             {
26                 scanf("%d", &sc);
27                 s1 += sc;
28                 map[i][j] = s1+map[i-1][j];
29             }
30         }
31         for(i = 1; i <= m; i++)
32             for(j = 1; j <= n; j++)
33                 if((i+x-1)<=m && (j+y-1)<=n)
34                 {
35                     ans = map[i+x-1][j+y-1]-map[i+x-1][j-1]-map[i-1][j+y-1]+map[i-1][j-1];//减出来就是以[i,j]
36                     //为左顶点,大小为xy的矩形的和
37                     if(Max < ans)
38                         Max = ans;
39                 }
40         printf("%d\n", Max);
41     }
42     return 0;
43 }

 

posted @ 2014-02-26 17:08  水门  阅读(155)  评论(0编辑  收藏  举报