【洛谷 1387】最大正方形

题目描述

在一个n*m的只包含0和1的矩阵里找出一个不包含0的最大正方形,输出边长。

输入格式

输入文件第一行为两个整数n,m(1<=n,m<=100),接下来n行,每行m个数字,用空格隔开,0或1.

输出格式

一个整数,最大正方形的边长

输入输出样例

输入 #1
4 4
0 1 1 1
1 1 1 0
0 1 1 0
1 1 0 1
输出 #1
2

题解;对没错,这是一道有价值的DPPPPPPP。只有这个值不为0,
就能说明这个以这个点右下端算出来的值,保证这块面积都为1
故可推出DP方程式:f[i][j]=min(f[i-1][j],min(f[i-1][j-1],f[i][j-1]))+1;
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
const int N=105;
int f[N][N],n,m,x,ans;
int main(){
    freopen("1387.in","r",stdin);
    freopen("1387.out","w",stdout);
    scanf("%d %d",&n,&m);
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            scanf("%d",&x);
            if(x==1) f[i][j]=min(f[i-1][j],min(f[i-1][j-1],f[i][j-1]))+1;
            ans=max(ans,f[i][j]);
        }
    }
    printf("%d
",ans);
    return 0;
}
原文地址:https://www.cnblogs.com/wuhu-JJJ/p/11679590.html