https://www.luogu.org/problemnew/show/P1387
题解:
动态规划,状转方程:
if (a[i][j]==1) f[i][j]=min(min(f[i][j-1],f[i-1][j]),f[i-1][j-1])+1;
说明:
f[i][j]表示以节点i,j为右下角,可构成的最大正方形的边长。
只有a[i][j]==1时,节点i,j才能作为正方形的右下角;
对于一个已经确定的f[i][j]=x,它表明包括节点i,j在内向上x个节点,向左x个节点扫过的正方形中所有a值都为1;
对于一个待确定的f[i][j],我们已知f[i-1][j],f[i][j-1],f[i-1][j-1]的值,如下:
f数组:
? ? ? ?
? ? 2 1
? ? 3 ?
? ? ? ?
则说明原a数组:
1 1 1 0
1 1 1 1
1 1 1 1
? ? ? ?
由此得出状态转移方程:
if (a[i][j]==1) f[i][j]=min(min(f[i][j-1],f[i-1][j]),f[i-1][j-1])+1;
for example:
a[i][j]:
0 0 0 1
1 1 1 1
0 1 1 1
1 1 1 1
f[i][j]:
0 0 0 1
1 1 1 1
0 1 2 2
1 1 2 3
/*
*@Author: STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
using namespace std;
typedef long long ll;
typedef __int128 lll;
const int N=1000;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,q,ans;
int a[N][N];
int f[N][N];
int main()
{
#ifdef DEBUG
freopen("input.in", "r", stdin);
//freopen("output.out", "w", stdout);
#endif
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
scanf("%d",&a[i][j]);
if(a[i][j])f[i][j]=min(min(f[i][j-1],f[i-1][j]),f[i-1][j-1])+1;
ans=max(ans,f[i][j]);//同步更新答案
}
}
cout<<ans<<endl;
//cout << "Hello world!" << endl;
return 0;
}