2548. 【NOIP2011模拟9.4】最大正方形 (StandardIO)

    xiaoxiao2025-01-11  7

    2548. 【NOIP2011模拟9.4】最大正方形 (StandardIO)

    Description

      给一个N*N的01矩阵, 求一个面积最大的全为1的正方形子矩阵. 输出它的面积.

    Input

      输入文件square.in的第一行包含一个正整数N.   接下来N行, 每行N个数, 保证不是0就是1. 每行相邻两个数之间没有空格.

    Output

      输出文件为square.out,仅包含一个整数表示最大的全1子正方形矩阵的面积。

    Sample Input

    2

    11

    11

    Sample Output

    4

    Hint

    【数据规模和约定】   80%的数据中 N<=250;   100%的数据中 N <= 1000。

    思路:

    本题呢算简单的吧,我也不知道考的叫什么名字,反正就是一个矩阵先做前缀和,然后计算出每个边长的正方形,暴力枚举,然后用二分去枚举边

    const maxn=1000; var a:array [0..maxn+1,0..maxn+1] of longint; s:array [1..maxn] of ansistring; i,j,n,max:longint; function find(x:longint):boolean; var i,j:longint; begin find:=false; for i:=0 to n-x do for j:=0 to n-x do if (a[i+x,j+x]-a[i+x,j]-a[i,j+x]+a[i,j]=0) then exit(true); exit(false); end; procedure sort(l,r:longint); var i,j,mid:longint; begin if r-l<=1 then begin if find(r) then writeln(r*r) else if find(l) then writeln(l*l) else writeln(0); halt; end; mid:=(l+r) div 2; if find(mid) then begin sort(mid,r); end else sort(l,mid-1); end; begin readln(n); for i:=1 to n do readln(s[i]); for i:=1 to n do for j:=1 to n do if s[i,j]='0' then a[i,j]:=a[i-1,j]+a[i,j-1]-a[i-1,j-1]+1 else a[i,j]:=a[i-1,j]+a[i,j-1]-a[i-1,j-1]; sort(1,n); end.

    转载请注明原文地址: https://ju.6miu.com/read-1295375.html
    最新回复(0)