CodeForces 679C - Bear and Square Grid


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

思路:先预处理好连通块,然后就可以直接计数了。只是代码用了滑块来优化,就是每次删除掉最左边的一列,然后再加上最右边的一列来计算


#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int maxn = 510;
int n, k, tol = 1; 
char map[maxn][maxn] = { 0 };
int belong[maxn][maxn] = { 0 }, cnt[maxn*maxn] = { 0 };		//belong标记这个'.'的归属,cnt保存连通块中的点的个数
bool vis[maxn][maxn] = { 0 };
int sum[maxn][maxn] = { 0 };			//sum[x][y]保存以(x,y)为左下端点的矩阵中点的个数
int cntc[maxn*maxn] = { 0 };			//记录某个连通块被访问的次数,在add和del函数中可以看出
int now = 0;							//当前滑块的计数
const int dx[] = { 0, 0, -1, 1 };
const int dy[] = { -1, 1, 0, 0 };

void dfs(int x, int y)
{
	belong[x][y] = tol;
	cnt[tol]++;
	vis[x][y] = true;
	for (int i = 0; i < 4; i++)
	{
		int nx = x + dx[i];
		int ny = y + dy[i];
		if (nx<1 || nx>n || ny<1 || ny>n) continue;
		if (map[nx][ny] == '.'&&!vis[nx][ny])
			dfs(nx, ny);
	}
}

void add(int x, int y)
{
	if (map[x][y] != '.') return;
	int c = belong[x][y];
	if (!cntc[c])	//这个联通块没有加进去
		now += cnt[c];
	cntc[c]++;		//统计次数加一
}

void del(int x, int y)
{
	if (map[x][y] != '.') return;
	int c = belong[x][y];
	cntc[c] --;
	if (!cntc[c])
		now -= cnt[c];
}

//计算边长为k,左上端点x,y的矩阵中,有多少个点
int cal(int x, int y)
{
	return sum[x + k - 1][y + k - 1] - sum[x + k - 1][y - 1] - sum[x - 1][y + k - 1] + sum[x - 1][y - 1];
}

void init()
{
	memset(vis, 0, sizeof(vis));
	memset(cnt, 0, sizeof(cnt));
	memset(cntc, 0, sizeof(cntc));
	memset(belong, 0, sizeof(belong));
	memset(sum, 0, sizeof(sum));
	memset(map, 0, sizeof(map));
	now = 0;
	tol = 1;
}

int main()
{
	while (scanf("%d%d", &n, &k) != EOF)
	{
		init();
		for (int i = 1; i <= n; i++)
			scanf("%s", map[i] + 1);
		//计算sum数组
		for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
		{
			sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1];
			if (map[i][j] == '.') sum[i][j]++;
		}
		//记录连通块
		for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
		{
			if (map[i][j] == '.'&&!vis[i][j])
			{
				dfs(i, j);
				tol++;
			}
		}

		//枚举左上端点求正解
		int ans = 0;
		for (int i = 1; i <= n - k + 1; i++)	//枚举行号
		{
			//计算最左边第一个矩阵的值
			memset(cntc, 0, sizeof(cntc));
			now = 0;
			for (int j = i - 1; j <= i + k; j++)   //行号
			{
				for (int l = 1; l <= k; l++)
				{
					add(j, l);
				}
			}
			for (int j = i; j < i + k; j++)
				add(j, k + 1);
			ans = max(ans, now + k*k - cal(i, 1));

			for (int j = 2; j <= n - k + 1; j++)	//枚举列号
			{
				for (int l = i; l < i + k; l++)
				{
					del(l, j - 2);
					add(l, j + k);
				}
				del(i - 1, j - 1); del(i + k, j - 1);
				add(i - 1, j + k - 1); add(i + k, j + k - 1);
				ans = max(ans, now + k*k - cal(i, j));
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值