Title Description
In a n*m-only matrix containing 0 and 1, find a maximum square that does not contain 0, and the output edge length.
Input/output format
Input format:
The first behavior of the input file is two integers n,m (1<=n,m<=100), followed by n lines, with m numbers per line, separated by spaces, 0 or 1.
Output format:
An integer, the edge length of the largest square
Input/Output sample
Input Sample # #:
4 40 1 1 11 1 1 00 1 1 01 1 0 1
Sample # # of output:
2
Ideas
DP,F[I,J] Indicates the square size with i,j as the rightmost bottom point.
F[i,j]:=min (min (f[i-1,j],f[i,j-1]), f[i-1,j-1]) +1
varA,f:Array[0.. +,0.. +] ofLongint; N,m,i,j,ans:longint; functionMax (x,y:longint): Longint; begin ifX>y ThenExit (x)Elseexit (y); End; functionmin (x,y:longint): Longint; begin ifX>y ThenExit (Y)Elseexit (x); End; beginFillchar (A,sizeof (a),0); Fillchar (F,sizeof (f),0); READLN (N,M); fori:=1 toN Do forj:=1 toM Doread (a[i,j]); fori:=1 toN Do forj:=1 toM Do ifa[i,j]=1 ThenF[i,j]:=min (min (f[i-1, j],f[i,j-1]), f[i-1, J-1])+1; fori:=1 toN Do forj:=1 toM Doans:=Max (ans,f[i,j]); Writeln (ANS); End.
View Code
[Luo gu 1387] the largest square