您的位置:首页 > 其它

UVALive 4867 Maximum Square 贪心

2015-01-21 10:32 459 查看
E - Maximum Square
Time Limit:4500MS Memory Limit:0KB 64bit IO Format:%lld & %llu
Submit Status Practice UVALive 4867

Description




Given an NxM matrix of all 1s and 0s, find the largest submatrix which is a square containing all 1s.

Input

There will be several test cases in the input. Each test case will begin with two integers, N and M(1

N, M

1, 000) indicating the number of rows and columns of the matrix. The next N lines will each contain M space-separated integers, guaranteed to be either 0 or 1. The input will end with a line with two 0s.

Output

For each test case, print a single integer, indicating the width (and height) of the largest square of all 1s, or 0 if there are no 1s. Print no extra spaces, and do not print any blank lines between answers.

Sample Input

4 5
0 1 0 1 1
1 1 1 1 1
0 1 1 1 0
1 1 1 1 1
3 4
1 1 1 1
1 1 1 1
1 1 1 1
6 6
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0


Sample Output

3
3
0


#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 1010
const int inf=0x7fffffff;   //无限大
int g[maxn][maxn];
int main()
{
int m,n;
while(cin>>n>>m)
{
if(m==0&&n==0)
break;
memset(g,0,sizeof(g));
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cin>>g[i][j];
}
}
int ans=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(i==0||j==0)
{
ans=max(g[i][j],ans);
continue;
}
if(g[i][j]==0)
continue;
g[i][j]=min(g[i-1][j],min(g[i][j-1],g[i-1][j-1]))+1;
ans=max(g[i][j],ans);
}
}

cout<<ans<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: