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. 3.sass的数据类型与函数

    数据类型 在sass里有数字.字符串.列表.颜色等类型 在cmd里 输入 sass -i 就会进入到交互模式,输入的计算可以马上得到结果 type-of()可以用来得到数据类型,如: type-of( ...

  2. Oracle数据库中插入日期型数据(to_date的用法)(转载)

    往Oracle数据库中插入日期型数据(to_date的用法) INSERT  INTO  FLOOR  VALUES  ( to_date ( '2007-12-20 18:31:34' , 'YYY ...

  3. 手动编译protobuf3的C++源码

    Windows下编译 官方文档 第三方文档 准备工具 Visual Studio 2013 CMake https://cmake.org/ Git https://git-scm.com/ 需要注意 ...

  4. Android 7.1 WindowManagerService 屏幕旋转流程分析 (三)

    三.屏幕的绘制 performSurfacePlacement()函数来触发window的绘制,这里最大的循环次数是6,当然一般不会到最大次数就会被Scheduled. final void perf ...

  5. HBase跨地区机房的压测小程序——从开发到打包部署(图文版)

    今天做了一个跨地区机房的压测小程序,主要的思路就是基于事先准备好的rowkey文件,利用多线程模拟并发的rowkey查询,可以实现并发数的自由控制.主要是整个流程下来,遇到了点打包的坑,所以特意记录下 ...

  6. Django学习日记01_环境搭建

    1. 使用Vagrant 创建ubuntu虚拟机: 首先安装vagrant,网上有比较多的方法,如:http://www.th7.cn/system/mac/201405/55421.shtml 我使 ...

  7. Head First设计模式之代理模式

    一.定义 定义:为其他对象提供一种代理以控制对这个对象的访问 在代理模式中,我们创建具有现有对象的对象,以便向外界提供功能接口. 二.结构 代理模式一般会有三个角色: 抽象角色(Subject):指代 ...

  8. 析构函数(C#)

    析构函数又称终结器,用于析构类的实例. 定义 析构函数(destructor) 与构造函数相反,当对象结束其生命周期时(例如对象所在的函数已调用完毕),系统自动执行析构函数.析构函数往往用来做&quo ...

  9. java基础只关键字final

    final关键字简述 final关键字是在编写java程序中出现频率和很高的关键字,如果想要更好的编写java程序,那么掌握final关键字的运用是非常必要的.让我们先看一下final关键字可以修饰的 ...

  10. scrapy使用PhantomJS爬取数据

    环境:python2.7+scrapy+selenium+PhantomJS 内容:测试scrapy+PhantomJS 爬去内容:涉及到js加载更多的页面 原理:配置文件打开中间件+修改proces ...