首先特判全部都是A或者全部都是B或者$n=1$的情况。

然后把矩阵四周都填充上A,枚举一个块,分以下情况讨论:

1.在它四周选两个块扩展,此时平方暴力枚举即可。

2.在它四周选定一个方向扩展两步。

3.选择一个角落,斜着扩展一步,再扩展另一步。

时间复杂度$O(n^2)$。

#include<cstdio>
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define N 1010
int n,m,i,j,x,y,l,r,f[N][N],ans,t,q[10000][3],cnt;bool a[N][N];char ch[N];
struct P{int x,y,l,r,s;}s[N*N];
inline void up(int b){if(ans<b)ans=b;}
inline void add(int x,int y,int z){
if(x&&x==y)return;
q[++cnt][0]=x,q[cnt][1]=y,q[cnt][2]=z;
}
int main(){
scanf("%d",&n);gets(ch);
if(n==1)return printf("1",ans),0;
rep(i,1,n)for(gets(ch+1),j=1;j<=n;j++)if(ch[j]=='B')a[i][j]=1;
rep(i,1,n)rep(j,1,n)if(a[i][j]&&!f[i][j]){
for(x=i;a[x][j];x++);
for(y=j;a[i][y];y++);
s[++m].x=i,s[m].y=--x,s[m].l=j,s[m].r=--y;
up(s[m].s=(x-i+1)*(y-j+1));
rep(I,i,x)rep(J,j,y)f[I][J]=m;
}
if(ans==n*n)return printf("%d",ans),0;
for(i=n;~i;i--)for(j=n;~j;j--)f[i+1][j+1]=f[i][j];
for(i=1;i<=m;i++)s[i].x++,s[i].y++,s[i].l++,s[i].r++;
n+=2;
ans=2;
rep(i,1,m){
x=s[i].x,y=s[i].y,l=s[i].l,r=s[i].r,cnt=0;
if(x>2)rep(j,l,r){
if(f[x-2][j])add(f[x-2][j],0,0);
else{
t=s[i].s+2+s[f[x-2][j-1]].s+s[f[x-2][j+1]].s;
if(f[x-2][j-1]!=f[x-1][j-1])t+=s[f[x-1][j-1]].s;
if(f[x-2][j+1]!=f[x-1][j+1])t+=s[f[x-1][j+1]].s;
if(x>3)t+=s[f[x-3][j]].s;
up(t);
}
}
if(y+2<=n)rep(j,l,r){
if(f[y+2][j])add(f[y+2][j],0,0);
else{
t=s[i].s+2+s[f[y+2][j-1]].s+s[f[y+2][j+1]].s;
if(f[y+2][j-1]!=f[y+1][j-1])t+=s[f[y+1][j-1]].s;
if(f[y+2][j+1]!=f[y+1][j+1])t+=s[f[y+1][j+1]].s;
if(y+3<=n)t+=s[f[y+3][j]].s;
up(t);
}
}
if(l>2)rep(j,x,y){
if(f[j][l-2])add(f[j][l-2],0,0);
else{
t=s[i].s+2+s[f[j-1][l-2]].s+s[f[j+1][l-2]].s;
if(f[j-1][l-2]!=f[j-1][l-1])t+=s[f[j-1][l-1]].s;
if(f[j+1][l-2]!=f[j+1][l-1])t+=s[f[j+1][l-1]].s;
if(l>3)t+=s[f[j][l-3]].s;
up(t);
}
}
if(r+2<=n)rep(j,x,y){
if(f[j][r+2])add(f[j][r+2],0,0);
else{
t=s[i].s+2+s[f[j-1][r+2]].s+s[f[j+1][r+2]].s;
if(f[j-1][r+2]!=f[j-1][r+1])t+=s[f[j-1][r+1]].s;
if(f[j+1][r+2]!=f[j+1][r+1])t+=s[f[j+1][r+1]].s;
if(r+3<=n)t+=s[f[j][r+3]].s;
up(t);
}
}
if(f[x-1][l-1]){
if(x>2)add(f[x-1][l-1],f[x-2][l],0);
if(l>2)add(f[x-1][l-1],f[x][l-2],0);
}
if(f[y+1][l-1]){
if(y+2<=n)add(f[y+1][l-1],f[y+2][l],0);
if(l>2)add(f[y+1][l-1],f[y][l-2],0);
}
if(f[x-1][r+1]){
if(x>2)add(f[x-1][r+1],f[x-2][r],0);
if(r+2<=n)add(f[x-1][r+1],f[x][r+2],0);
}
if(f[y+1][r+1]){
if(y+2<=n)add(f[y+1][r+1],f[y+2][r],0);
if(r+2<=n)add(f[y+1][r+1],f[y][r+2],0);
}
if(x==y){
if(l>2)add(f[x][l-2],f[x-1][l-1],f[x+1][l-1]);
if(r+2<=n)add(f[x][r+2],f[x-1][r+1],f[x+1][r+1]);
}
if(l==r){
if(x>2)add(f[x-2][l],f[x-1][l-1],f[x-1][l+1]);
if(y+2<=n)add(f[y+2][l],f[y+1][l-1],f[y+1][l+1]);
}
up(s[i].s+2);
rep(j,1,cnt){
up(s[i].s+2+s[q[j][0]].s+s[q[j][1]].s+s[q[j][2]].s);
rep(k,j+1,cnt){
t=s[i].s+2+s[q[j][0]].s+s[q[j][1]].s+s[q[j][2]].s+s[q[k][0]].s+s[q[k][1]].s+s[q[k][2]].s;
if(q[k][0]==q[j][0]||q[k][0]==q[j][1]||q[k][0]==q[j][2])t-=s[q[k][0]].s;
if(q[k][1]==q[j][0]||q[k][1]==q[j][1]||q[k][1]==q[j][2])t-=s[q[k][1]].s;
if(q[k][2]==q[j][0]||q[k][2]==q[j][1]||q[k][2]==q[j][2])t-=s[q[k][2]].s;
up(t);
}
}
if(x>1&&l>1&&!f[x-1][l-1]){
t=s[i].s+2+s[f[x-2][l-1]].s+s[f[x-1][l-2]].s;
if(x==y&&l==r){
if(f[x-1][l-2]!=f[x][l-2])up(t+s[f[x][l-2]].s+s[f[x+1][l-1]].s);else up(t+s[f[x+1][l-1]].s);
if(f[x-2][l-1]!=f[x-2][l])up(t+s[f[x-2][l]].s+s[f[x-1][l+1]].s);else up(t+s[f[x-1][l+1]].s);
}else{
if(f[x-1][l-2]!=f[x][l-2])up(t+s[f[x][l-2]].s);
if(f[x-2][l-1]!=f[x-2][l])up(t+s[f[x-2][l]].s);
}
}
if(x>1&&r<n&&!f[x-1][r+1]){
t=s[i].s+2+s[f[x-2][r+1]].s+s[f[x-1][r+2]].s;
if(x==y&&l==r){
if(f[x-1][r+2]!=f[x][r+2])up(t+s[f[x][r+2]].s+s[f[x+1][l+1]].s);else up(t+s[f[x+1][l+1]].s);
if(f[x-2][r+1]!=f[x-2][r])up(t+s[f[x-2][r]].s+s[f[x-1][l-1]].s);else up(t+s[f[x-1][l-1]].s);
}else{
if(f[x-1][r+2]!=f[x][r+2])up(t+s[f[x][r+2]].s);
if(f[x-2][r+1]!=f[x-2][r])up(t+s[f[x-2][r]].s);
}
}
if(y<n&&l>1&&!f[y+1][l-1]){
t=s[i].s+2+s[f[y+2][l-1]].s+s[f[y+1][l-2]].s;
if(x==y&&l==r){
if(f[y+1][l-2]!=f[y][l-2])up(t+s[f[y][l-2]].s+s[f[x-1][l-1]].s);else up(t+s[f[x-1][l-1]].s);
if(f[y+2][l-1]!=f[y+2][l])up(t+s[f[y+2][l]].s+s[f[x+1][l+1]].s);else up(t+s[f[x+1][l+1]].s);
}else{
if(f[y+1][l-2]!=f[y][l-2])up(t+s[f[y][l-2]].s);
if(f[y+2][l-1]!=f[y+2][l])up(t+s[f[y+2][l]].s);
}
}
if(y<n&&r<n&&!f[y+1][r+1]){
t=s[i].s+2+s[f[y+2][r+1]].s+s[f[y+1][r+2]].s;
if(x==y&&l==r){
if(f[y+1][r+2]!=f[y][r+2])up(t+s[f[y][r+2]].s+s[f[x-1][l+1]].s);else up(t+s[f[x-1][l+1]].s);
if(f[y+2][r+1]!=f[y+2][r])up(t+s[f[y+2][r]].s+s[f[x+1][l-1]].s);else up(t+s[f[x+1][l-1]].s);
}else{
if(f[y+1][r+2]!=f[y][r+2])up(t+s[f[y][r+2]].s);
if(f[y+2][r+1]!=f[y+2][r])up(t+s[f[y+2][r]].s);
}
}
}
return printf("%d",ans),0;
}

  

BZOJ4348 : [POI2016]Park wodny的更多相关文章

  1. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  2. hdu4607 Park Visit(树的直径)

    Park Visit Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  3. Codeforces Round #292 (Div. 1) C. Drazil and Park 线段树

    C. Drazil and Park 题目连接: http://codeforces.com/contest/516/problem/C Description Drazil is a monkey. ...

  4. 转:php park、unpark、ord 函数使用方法(二进制流接口应用实例)

    在工作中,我也逐渐了解到park,unpark,ord对于二进制字节处理的强大. 下面我逐一介绍它们.     park,unpark,ord这3个函数,在我们工作中,用到它们的估计不多. 我在最近一 ...

  5. Kefa and Park

    #include<bits/stdc++.h> #define max 100005 using namespace std; int cats[max]; vector<int&g ...

  6. UVALive 4221 Walk in the Park 扫描线

    Walk in the Park 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemi ...

  7. B. Om Nom and Dark Park

    B. Om Nom and Dark Park 在满二叉树上的某些边上添加一些值.使得根节点到叶子节点的路径上的权值和都相等.求最少需要添加多少. 我们利用性质解题.   考察兄弟节点.由于他们从跟节 ...

  8. Codeforces Round #321 (Div. 2) C. Kefa and Park dfs

    C. Kefa and Park Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/probl ...

  9. LockSupport的park和unpark

    LockSupport是JDK中比较底层的类,用来创建锁和其他同步工具类的基本线程阻塞原语. Java锁和同步器框架的核心AQS:AbstractQueuedSynchronizer,就是通过调用Lo ...

随机推荐

  1. ios socket(基础demo)

    http://blog.sina.com.cn/s/blog_7a2f0a830101ecv4.html clinetSocket 1.viewcontroller.h @interface View ...

  2. linux下vim命令详解 转自: zhanglong0426

      高级一些的编辑器,都会包含宏功能,vim当然不能缺少了,在vim中使用宏是非常方便的: :qx     开始记录宏,并将结果存入寄存器xq     退出记录模式@x     播放记录在x寄存器中的 ...

  3. PHP 基础语法实例及注意事项

    <?$varint = 1;$varinteger = "test";$varstring = "tes";$varbool = true;$varflo ...

  4. Java数据库ResultSet转json实现

    现在有很多json相关的Java工具,如json-lib.gson等,它们可以直接把JavaBean转换成json格式. 在开发中,可能会从数据库中获取数据,希望直接转成json数组,中间不通过bea ...

  5. DICOM:Ubuntu14环境下安装dcm4chee+oviyam2.1

    背景: 之前博文DICOM:开源DICOM服务框架DCM4CHEE安装中曾介绍过在Windows7环境下安装部署dcm4chee.近期公司项目需要与部署到Linux系统的远端基于dcm4chee框架的 ...

  6. poj2485 Highways

    Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public h ...

  7. ICMP-type对应表

    一次在某个防火墙配置策略里看到如下的代码: iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT iptables -A FORWARD -p icmp ...

  8. ubuntu14.04安装OpenVirteX

    官网链接: http://ovx.onlab.us/getting-started/installation/ step1: System requirements: Recommended 4 Co ...

  9. POJ 2418

    http://poj.org/problem?id=2418 这是一个二叉树的题目,但我看了下书,还是不是特别理解会用二叉树,所以我就用其他的办法来做,结果一样AC,时间也就1700多ms,比起二叉树 ...

  10. maven项目 Java compiler level does not match the version of the installed Java project facet

    因工作的关系,Eclipse开发的Java项目拷来拷去,有时候会报一个很奇怪的错误.明明源码一模一样,为什么项目复制到另一台机器上,就会报“java compiler level does not m ...