题目描述 Description
在一个01矩阵中,包含有很多的正方形子矩阵,现在要求出这个01矩阵中,最大的正方形子矩阵,使得这个正方形子矩阵中的某一条对角线上的值全是1,其余的全是0。
输入描述 Input Description
第一行有两个整数n和m(1<=n,m<=1000)。接下来的n行,每行有m个0或1的数字。每两个数字之间用空格隔开。
输出描述 Output Description
只有一个整数,即这个满足条件的最大的正方形子矩阵的边长。
样例输入 Sample Input
4 6
0 1 0 1 0 0
0 0 1 0 1 0
1 1 0 0 0 1
0 1 1 0 1 0
样例输出 Sample Output
3
#include<cstdio>
#include<iostream>
#include <queue>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
int map[1002][1002],shu[1002][1002],heng[1002][1002],s[1002][1002],max1=0,n,m;
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++){
cin>>map[i][j];
if(map[i][j]==0){
shu[i][j]=shu[i-1][j]+1;
heng[i][j]=heng[i][j-1]+1;
}
}
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++){
if(map[i][j]==1){
s[i][j]=min(s[i-1][j-1],min(shu[i-1][j],heng[i][j-1]))+1;
max1=max(max1,s[i][j]);
}
}
int henga;
for(int i=1;i<=n;i++)
{
henga=0;
for(int j=m;j>=1;j--){
if(map[i][j]==1){
s[i][j]=min(s[i-1][j+1],min(shu[i-1][j], henga ) )+1;
max1=max(max1,s[i][j]);
henga=0;
}
else henga++;
}
}
cout<<max1;
return 0;
}

3406

被折叠的 条评论
为什么被折叠?



