Problem H: Qz’s Maximum All One Square

Problem H: Qz’s Maximum All One Square

Time Limit: 5 Sec   Memory Limit: 128 MB
Submit: 67   Solved: 17
[ Submit][ Status][ Web Board]

Description

YH gave Qz an odd matrix consists of one or zero. He asked Qz to find a square that has the largest area. The problem is not so easy, that means the square you find must not contain any zero. Your task is finding the square and you only need to output the area of the matrix. If you help Qz, he will thanks you a lot and treat you a meal. Please call him after the contest if you solve this problem. His number is XXXXXXXXXXXX. Hehe~

Input

For each case, Qz first give you 2 number n and m (1 <= n <= 1000, 1 <= m <= 1000),the matrix is in size of n columns and m rows.
Then Qz will give you the matrix.  To avoid cost his money, Qz decided to make this problem a little difficult... Do not worry, he just gave you multiple cases.

Output

For each test case, output the desired answer.

Sample Input

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

Sample Output

4
4

HINT

题意:大概是给出一个只含0,1的m*n矩阵(注意是m行n列,我就是错这了不知道为什么总A不了,蛋疼)的矩阵。求其面积内全为1最大的正方形面积。
我的想法是:这个题,如果我题意没理解错的话,应该就是有一个只有数字0,1的n*m的矩阵,求全为1的最大正方形面积。
我是这么想的也是这么做的:
首先构建一个a[i][j]的矩阵。从i=1 to n; j=1 to m;枚举每一个数字,如果为1,则开始进入判断。将其边长+1,看是否满足增加的2边是否为1,另外再判断另外两边是否为1,若都为1则说明,则继续将边长+1.并一边记录最大边长s。在一行的终止条件可以为m-j+1<=s则退出这一层循环,因为后面再增加的边,也不可能会大于当前最大边s,所以不必要做无用功。同理列的终止条件也可为n-i+1<=s。这样就优化了循环次数。最后再输出s*s即是最大面积。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <vector>
using namespace std;
int n,m,i,j,l;
int a[1010][1010];
int main()
{
	while (~scanf("%d%d",&m,&n))
	{
		memset(a,0,sizeof(a));
		for (i=1;i<=n;i++)
			for (j=1;j<=m;j++)
				scanf("%d",&a[i][j]);
		long long s=0;		
		for (i=1;i<=n;i++)
		{
			if (n-i+1<=s) break;
			for (j=1;j<=m;j++)
			{
				if (m-j+1<=s) break;
				if (a[i][j])
				{
					int k=1;	
					if (s<k) s=k;
					int j2=j+1;
					while (a[i][j2] && a[i+k][j])
					{
						int flag=1;
						for (l=1;l<=k;l++)
							if (a[i+k][j+l]==0 || a[i+l][j2]==0)
							{
								flag=0;
								break;
							}
						if (!flag) break;
						k++;
						if (s<k) s=k;
						j2++;			
					}
				}
			}
		}
		printf("%lld\n",s*s);
	} 
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值