C. Bear and Square Grid
time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You have a grid with n rows and n columns. Each cell is either empty (denoted by '.') or blocked (denoted by 'X').

Two empty cells are directly connected if they share a side. Two cells (r1, c1) (located in the row r1 and column c1) and (r2, c2) areconnected if there exists a sequence of empty cells that starts with (r1, c1), finishes with (r2, c2), and any two consecutive cells in this sequence are directly connected. A connected component is a set of empty cells such that any two cells in the component are connected, and there is no cell in this set that is connected to some cell not in this set.

Your friend Limak is a big grizzly bear. He is able to destroy any obstacles in some range. More precisely, you can choose a square of size k × k in the grid and Limak will transform all blocked cells there to empty ones. However, you can ask Limak to help only once.

The chosen square must be completely inside the grid. It's possible that Limak won't change anything because all cells are empty anyway.

You like big connected components. After Limak helps you, what is the maximum possible size of the biggest connected component in the grid?

Input

The first line of the input contains two integers n and k (1 ≤ k ≤ n ≤ 500) — the size of the grid and Limak's range, respectively.

Each of the next n lines contains a string with n characters, denoting the i-th row of the grid. Each character is '.' or 'X', denoting an empty cell or a blocked one, respectively.

Output

Print the maximum possible size (the number of cells) of the biggest connected component, after using Limak's help.

Examples
input
5 2
..XXX
XX.XX
X.XXX
X...X
XXXX.
output
10
input
5 3
.....
.XXX.
.XXX.
.XXX.
.....
output
25
Note

In the first sample, you can choose a square of size 2 × 2. It's optimal to choose a square in the red frame on the left drawing below. Then, you will get a connected component with 10 cells, marked blue in the right drawing.

炸掉任意一个k*k的矩阵内所有方块,然后问炸完最大的联通块的最大值。

n*n枚举我现在要炸哪个矩阵,由于每次枚举是把矩阵右移一位,我们可以考虑利用上次的信息。

(我都扯了些什么鬼……

反正就是每次删掉一列的信息,增加一列的信息而已。

预处理出原先的联通块,然后开个桶记录一下每个联通块被我现在的矩形覆盖的点数,当这个点数从0加到1时就给答案累计上这个联通块的大小,从1减到0就扔掉就好了。

#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int read_p,read_ca;
inline int read(){
read_p=;read_ca=getchar();
while(read_ca<''||read_ca>'') read_ca=getchar();
while(read_ca>=''&&read_ca<='') read_p=read_p*+read_ca-,read_ca=getchar();
return read_p;
}
const int f[][]={{,},{,-},{,},{-,}};
int n,K,map[][],nm=,t[],mmh,MMH=,nu[];
char c[];
struct na{
int x,y;
na(int _x,int _y):x(_x),y(_y){}
};
queue<na> q;
inline void bfs(int x,int y){
register int i;
map[x][y]=++nm;
q.push(na(x,y));
while (!q.empty()){
nu[nm]++;
na k=q.front();q.pop();
for (i=;i<;i++)
if (map[k.x+f[i][]][k.y+f[i][]]==) map[k.x+f[i][]][k.y+f[i][]]=nm,q.push(na(k.x+f[i][],k.y+f[i][]));
}
}
int main(){
register int i,j,k;
n=read();K=read();
for (i=;i<=n;i++){
scanf("%s",c);
for (j=;j<=n;j++) map[i][j]=c[j-]=='X'?-:;
}
for (i=;i<=n;i++) map[i][]=map[i][n+]=-;
for (j=;j<=n;j++) map[][j]=map[n+][j]=-;
for (i=;i<=n;i++)
for (j=;j<=n;j++) if (map[i][j]==) bfs(i,j);
for (i=;i<=n-K+;i++){
memset(t,,sizeof(t));mmh=;
for (j=i;j<i+K;j++)
for (k=;k<=K;k++) if (map[j][k]==-) mmh++;else{
if ((++t[map[j][k]])==) mmh+=nu[map[j][k]];
}
for (k=;k<=K;k++){
if (map[i-][k]!=-) if ((++t[map[i-][k]])==) mmh+=nu[map[i-][k]];
if (map[i+K][k]!=-) if ((++t[map[i+K][k]])==) mmh+=nu[map[i+K][k]];
}
for (j=i;j<i+K;j++)
if (map[j][K+]!=-) if ((++t[map[j][K+]])==) mmh+=nu[map[j][K+]];
if (mmh>MMH) MMH=mmh;
for (k=;k<=n-K;k++){
for (j=i;j<i+K;j++){
if (map[j][k+K]==-) mmh++;
if (map[j][k+K+]!=-) if ((++t[map[j][k+K+]])==) mmh+=nu[map[j][k+K+]];
if (map[j][k]==-) mmh--;
if (map[j][k-]!=-) if ((--t[map[j][k-]])==) mmh-=nu[map[j][k-]];
}
if (map[i-][k+K]!=-) if ((++t[map[i-][k+K]])==) mmh+=nu[map[i-][k+K]];
if (map[i-][k]!=-) if ((--t[map[i-][k]])==) mmh-=nu[map[i-][k]];
if (map[i+K][k+K]!=-) if ((++t[map[i+K][k+K]])==) mmh+=nu[map[i+K][k+K]];
if (map[i+K][k]!=-) if ((--t[map[i+K][k]])==) mmh-=nu[map[i+K][k]];
if (mmh>MMH) MMH=mmh;
}
}
printf("%d\n",MMH);
}

Codeforces Round #356 (Div. 1) C. Bear and Square Grid的更多相关文章

  1. Codeforces Round #356 (Div. 2) E. Bear and Square Grid 滑块

    E. Bear and Square Grid 题目连接: http://www.codeforces.com/contest/680/problem/E Description You have a ...

  2. Codeforces Round #356 (Div. 2) C. Bear and Prime 100(转)

    C. Bear and Prime 100 time limit per test 1 second memory limit per test 256 megabytes input standar ...

  3. Codeforces Round #356 (Div. 2)B. Bear and Finding Criminals(水题)

    B. Bear and Finding Criminals time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  4. Codeforces Round #356 (Div. 2)A. Bear and Five Cards(简单模拟)

    A. Bear and Five Cards time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  5. Codeforces Round #356 (Div. 1) D. Bear and Chase 暴力

    D. Bear and Chase 题目连接: http://codeforces.com/contest/679/problem/D Description Bearland has n citie ...

  6. Codeforces Round #356 (Div. 2) D. Bear and Tower of Cubes dfs

    D. Bear and Tower of Cubes 题目连接: http://www.codeforces.com/contest/680/problem/D Description Limak i ...

  7. Codeforces Round #356 (Div. 2) C. Bear and Prime 100 水题

    C. Bear and Prime 100 题目连接: http://www.codeforces.com/contest/680/problem/C Description This is an i ...

  8. Codeforces Round #356 (Div. 2) B. Bear and Finding Criminal 水题

    B. Bear and Finding Criminals 题目连接: http://www.codeforces.com/contest/680/problem/B Description Ther ...

  9. Codeforces Round #356 (Div. 2) A. Bear and Five Cards 水题

    A. Bear and Five Cards 题目连接: http://www.codeforces.com/contest/680/problem/A Description A little be ...

随机推荐

  1. CentOS ifconfig无IP地址解决办法

    修改/etc/sysconfig/network-scripts/ifcfg-ens33 把 ONBOOT 改为 yes,重启后就会有ip,且物理机和虚拟机可以互相ping通了.

  2. 33 款主宰 2017 iOS 开发的开源库

    推荐一篇文章 改文章汇聚了现在主流的一些三方框架,很值得一看 https://mp.weixin.qq.com/s/ICodliohtzbmA-eLKRFT-Q

  3. VR、AR、MR定义区别

    近日, 获得谷歌5亿美元融资的技术公司Magic Leap在WSJD展会中放出了一段实录视频,引起不小骚动.如今,也有媒体称他们为MR公司,那么VR.AR.MR之间到底有什么区别呢. VR.AR.MR ...

  4. 服务器端语言go之开篇分享

    由于之前看过其他脚本语言,此时看服务器端语言go语法时也短短用了半天的时间,如图1所示,是个人学习go语法的目录截图,学习网址:菜鸟网站,为了个人方便学习和记忆,因此写下本篇文章,在本篇文章里我主要是 ...

  5. Effective Java 第三版——12. 始终重写 toString 方法

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

  6. Webpack 2 视频教程 010 - 配置 ESLint 实现代码规范自动测试 (下)

    原文发表于我的技术博客 这是我免费发布的高质量超清「Webpack 2 视频教程」. Webpack 作为目前前端开发必备的框架,Webpack 发布了 2.0 版本,此视频就是基于 2.0 的版本讲 ...

  7. vim编辑器的使用技巧

    vim(vi)是上Linux非常常用的编辑器,很多Linux发行版都默认安装了vi(vim).vi(vim)命令繁多但是如果使用灵活之后将会大大提高效率.vi是“visual interface”的缩 ...

  8. python科学计算_numpy_广播与下标

    多维数组下标 多维数组的下标是用元组来实现每一个维度的,如果元组的长度比维度大则会出错,如果小,则默认元组后面补 : 表示全部访问: 如果一个下标不是元组,则先转换为元组,在转换过程中,列表和数组的转 ...

  9. EditTable可编辑的表格

    EditTable可编辑的表格 EditTable基于tabel布局的表格,表格内容单击可以编辑,编辑完毕即可显示新的内容:    ESC按键可以撤销编辑,返回原有内容.    点击"添加& ...

  10. September,开启一个新的征程!

    寻找梦里的未来笑对现实的无奈不能后退的时候不再傍徨的时候永远向前 路...一直都在