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 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) are connected 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.
Sample Input
5 2
..XXX
XX.XX
X.XXX
X...X
XXXX.
Sample Output
10
Hint
题意
给你n*n的矩阵,你有一个k*k的框框,可以把矩阵的某一块给框起来
然后这个框框内的所有格子都是.了,现在你需要使得这个矩阵的连通块最大,那么这个连通块的大小是多少呢?
题解:
简单点,我们思考一下,最大的这个答案一定是这个框框所在的连通块,这个很显然,因为你加了框框之后,连通块的大小是会增加或者保持不变的。
所以我们直接暴力枚举所有框框摆放的位置就好了。
我们利用滑块的思想去做这道题,那么我们维护这个框框的时候,就只用维护他的边界信息了
这样就可以把n4->n3了。
然后这道题就可以AC了。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 505;
int n,k;
char grid[maxn][maxn];
int cc[maxn][maxn];
int cc_size[maxn*maxn];
int when_added[maxn*maxn];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,-1,1};
bool inside(int x,int y)
{
if(x<0||x>=n)return false;
if(y<0||y>=n)return false;
return true;
}
void dfs(int x,int y,int num)
{
cc[x][y]=num;
++cc_size[num];
for(int i=0;i<4;i++)
{
int x2=x+dx[i];
int y2=y+dy[i];
if(inside(x2,y2)&&grid[x2][y2]=='.'&&cc[x2][y2]==0)
dfs(x2,y2,num);
}
}
void add(int x,int y,int& ans,int num)
{
if(inside(x,y)&&grid[x][y]=='.')
{
int id = cc[x][y];
if(when_added[id]!=num)
{
when_added[id]=num;
ans+=cc_size[id];
}
}
}
void QAQ()
{
scanf("%d%d",&n,&k);
for(int i=0;i<n;i++)
scanf("%s",grid[i]);
int cnt = 0;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
if(grid[i][j]=='.'&&cc[i][j]==0)
dfs(i,j,++cnt);
int cur_time = 1;
int Ans = 0;
for(int y_low=0;y_low+k-1<n;y_low++)
{
for(int x=0;x<k;x++)
for(int y=y_low;y<y_low+k;y++)
--cc_size[cc[x][y]];
for(int x_low=0;x_low+k-1<n;x_low++)
{
int ans = k*k;
for(int x=x_low;x<x_low+k;x++)
{
add(x,y_low-1,ans,cur_time);
add(x,y_low+k,ans,cur_time);
}
for(int y=y_low;y<y_low+k;y++)
{
add(x_low-1,y,ans,cur_time);
add(x_low+k,y,ans,cur_time);
}
++cur_time;
Ans=max(Ans,ans);
if(x_low+k!=n)
{
for(int y=y_low;y<y_low+k;y++)
{
++cc_size[cc[x_low][y]];
--cc_size[cc[x_low+k][y]];
}
}
}
for(int x=n-k;x<n;x++)
for(int y=y_low;y<y_low+k;y++)
++cc_size[cc[x][y]];
}
cout<<Ans<<endl;
}
int main()
{
QAQ();
}
Codeforces Round #356 (Div. 2) E. Bear and Square Grid 滑块的更多相关文章
- Codeforces Round #356 (Div. 1) C. Bear and Square Grid
C. Bear and Square Grid time limit per test 3 seconds memory limit per test 256 megabytes input stan ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- #ifdef __cplusplus extern "C" { #endif”的定义的含义
看一些程序的时候老是有“#ifdef __cplusplusextern "C" {#endif”的定义,搞搞清楚是怎么回事: Microsoft-Specific Predefi ...
- [Android Studio] Android Studio如何快速生成get,set,tostring,构造函数
刚开始使用Android Studio时,在创建一个javabean时,很习惯的在JavaBean类中,右键去找生成get,set等选项.但是很遗憾,找不到. 那这边如何快速的set,get或者生成构 ...
- 如何提高单片机Flash的擦写次数
所谓提高flash的擦写次数,并不是真正的提高flash擦写次数,而是通过以"空间换时间"概念,在软件上实现“操作的次数大于其寿命”.详见链接: http://bbs.eeworl ...
- Southwestern Europe Regional Contest 2015 题解
题目链接:http://codeforces.com/gym/101128 题目数7/10 Rank 34/209 A: 题意:给出一张n个点的有向图表示一家有n个员工的公司的隶属图,u->v表 ...
- 002_IO磁盘深入理解
一.如何测试云硬盘 https://www.ustack.com/blog/how-benchmark-ebs/#fio
- 【前端】上拉加载更多dropload.min.js的使用
代码如下:入职代码修改接口及html为自己的即可(下面主要展示js部分) <!DOCTYPE html><html> <head> <meta charset ...
- elasticsearch学习笔记--原理介绍
前言:上一篇中我们对ES有了一个比较大概的概念,知道它是什么,干什么用的,今天给大家主要讲一下他的工作原理 介绍:ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户 ...
- <编程之美>经典面试题:求二叉树节点的最大距离(我的解法,最容易理解的版本?)
题目介绍: 如果把二叉树看成一个图,父子节点之间的连线看成是双向的,我们姑且定义"距离"为两个节点之间的个数. 写一个程序求一棵二叉树中相距最远的两个节点之间的距离. 如下图所示, ...
- win7下scheme环境配置
运行lisp方言--scheme,在windows下,用eclipse. 1.eclipse装好 2.eclipse安装插件scheme48 development took, http://www. ...
- git/github 生成密钥
当从本地提交文件到github的时候,提交不成功,报错,可能问题就是你还没有生成ssh秘钥 github要使用ssh密钥的原因: git使用https协议,每次pull, push都要输入密码,相当的 ...