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

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.


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值