Take the Land - UVa 10074 变形最大子矩阵和

本文讨论了一个算法问题,即在一个由1和0组成的矩阵中,找到包含最多0的最大矩形区域。通过遍历矩阵并计算每行连续0的长度,最终确定最大的空白矩形面积。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Take the Land

Input : standard input

Output : standard output

 

The poor man went to the King and said, “Lord, I cannot maintain my family. Please give me some wealth so that I can survive with my wife and children.” The King replied, “I shall grant you a piece of land so that you can cultivate and grow food for your family. In the southern part of the Kingdom there is a rectangular forest. Trees have been planted there at regular intervals. Some of the trees have been cut for use. You are allowed to take any rectangular piece of land that does not contain any tree. You need not go to the forest to select the piece of land. I have a map containing 1s at places where there is a tree and 0s at points where the tree has been cut.”

 

Help the poor man to find out the largest piece of land. Area of the land is measured in units of number of trees that were there. Your program should take a matrix of 1s and 0s as input and output the area of the largest rectangular piece of land that contain no tree. Be careful about the efficiency of your program.

 

Input

The input file may contain multiple test cases. The first line of each test case contains two integers M and N  (1<=M,N<=100) giving the number of rows and columns in the matrix that follows. Each of the next M lines contains N symbols (either 0 or 1). Two consecutive symbols in a line will be separated by a single space. The input terminates with two zeros for M and N.

 

Output

For each test case in the input print a line giving the area (in terms of the number of trees were there) of the largest rectangular piece of land containing no tree.

 

Sample Input

6 7
0 1 1 0 1 1 0
0 0 0 0 0 1 0
1 0 0 0 0 0 1
0 1 0 0 0 0 1
1 1 0 0 0 1 0
1 1 0 1 1 0 0
0 0

Sample Output

12


题意:找到最大的空白矩阵。

思路:遍历列,使其成为一位的,然后再求最长的连续0。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int tree[1010][1010];
int sum[1010][1010];
int main()
{ int i,j,k,n,m,ans,p;
  while(~scanf("%d%d",&n,&m) && n>0)
  { for(i=1;i<=n;i++)
     for(j=1;j<=m;j++)
      scanf("%d",&tree[i][j]);
    for(i=1;i<=n;i++)
     for(j=1;j<=m;j++)
      sum[i][j]=sum[i][j-1]+tree[i][j];
    ans=0;
    for(i=1;i<=m;i++)
     for(j=i;j<=m;j++)
     { p=0;
       for(k=1;k<=n;k++)
       { if(sum[k][j]-sum[k][i-1]>0)
         { ans=max(ans,(j-i+1)*p);
           p=0;
         }
         else
          p++;
       }
       ans=max(ans,(j-i+1)*p);
     }
    printf("%d\n",ans);
  }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值