HIT2000 Maximum Submatrix(最大连续0子矩阵面积)

继续补题……

 

题目链接:

  http://acm.hit.edu.cn/hoj/problem/view?id=2000

题目描述:

Maximum Submatrix

 

Have you ever watched the movie Matrix ? In that movie, the term Matrix does not mean a mathematical thing, but a complicated AI system.

"""

In this problem, we will go back to the original meaning of matrix. Given a 0-1 matrix, you are required to find the maximum submatrix in it which contains only 0s.

Input

An integer T (T <= 110) in the first line indicates the number of test case. For each test case, two integers N and M will be given first, then followed by a N * M 0-1 matrix.

1 <= N, M <= 100.

Output

For each test case, print the number of elements in the maximum submatrix in a single line.

Sample Input

2
2 2
0 0
0 0
4 5
1 0 1 0 0
0 1 0 0 0
0 0 1 0 0
1 1 0 0 0

Sample Output

4
8

题目大意:
  找出最大的连续子矩阵使得其中元素均为0
思路:
  dp
  记录每列的前缀和,这样两数相减即可得到一列中任意连续数的和
  二重for循环控制行的范围i~j,然后在这个范围中对于列贪心求解,类似于连续最大序列和
  
  复杂度O(n^3),100组数据,一秒内可做

代码:
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 const int N = 110;
 7 
 8 int main() {
 9     int t, m, n, ans, num[N][N], sum[N][N] = { 0 };
10     cin >> t;
11     while (t--) {
12         ans = 0;
13         scanf("%d%d", &m, &n);
14         for (int i = 1; i <= m; ++i)
15             for (int j = 1; j <= n; ++j) {
16                 scanf("%d", &num[i][j]);
17                 sum[i][j] = sum[i - 1][j] + num[i][j];    //记录一列中的前缀和
18             }
19         for (int i = 0; i < m; ++i)
20             for (int j = i + 1; j <= m; ++j) {    //控制行
21                 int m_sum = 0, po = 1, tmp = 0;
22                 for (int k = 1; k <= n; ++k) {    //贪心求解
23                     m_sum += sum[j][k] - sum[i][k];
24                     if (m_sum)
25                         tmp = 0, po = k + 1, m_sum = 0;
26                     else
27                         tmp += j - i, ans = max(ans, tmp);
28                 }
29             }
30         printf("%d\n", ans);
31     }
32 }
 
  

 

 

 

 

转载于:https://www.cnblogs.com/hyp1231/p/6965538.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值