CodeForces 679C - Bear and Square Grid
来源:互联网 发布:当今网络环境 编辑:程序博客网 时间:2024/06/11 02:21
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?
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.
Print the maximum possible size (the number of cells) of the biggest connected component, after using Limak's help.
5 2..XXXXX.XXX.XXXX...XXXXX.
10
5 3......XXX..XXX..XXX......
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;}
- CodeForces 679C - Bear and Square Grid
- Codeforces 679C Bear and Square Grid(枚举 标记)
- Codeforces 679C Bear and Square Grid(滑动窗口)
- Codeforces 679C Bear and Square Grid 暴力(滑动窗口)
- Codeforces Round #356 (Div. 2) Bear and Square Grid
- Codeforces Round #356 (Div. 2)E. Bear and Square Grid
- Codeforces Round #356 (Div. 2) Bear and Square Grid ★ ★ ★
- Codeforces Round #356 (Div. 2) E. Bear and Square Grid 滑块
- codeforces_679C. Bear and Square Grid(滑块)
- Codeforces 385 C. Bear and Prime Numbers
- CodeForces 385C Bear and Prime Numbers
- Codeforces 385C Bear and Prime Numbers
- Codeforces 385 C Bear and Prime Numbers
- codeforces 385C Bear and Prime Numbers
- CodeForces 574C - Bear and Poker
- Codeforces Round #318 C - Bear and Poker
- CodeForces #318 (div1) C.Bear and Drawing
- Codeforces Round #318-(C. Bear and Poker)
- JavaSE_02基本语法
- 字符串分隔
- 【附】使用监听器ServletContextListener实现网站登录人数统计,可以实现重启服务器后记录仍然存在
- ping 命令中的 TTL
- AttributeError: module 'html5lib.treebuilders' has no attribute '_base'
- CodeForces 679C - Bear and Square Grid
- bss、data和rodata区别与联系
- 怎样量化评价搜索引擎的结果质量
- Inspirations to be better
- postgresql pgagent 的安装及使用(linux 5.8)
- Android 触摸事件解析( 耐心看完就懂了哦)
- 软件设计之——“高内聚低耦合”
- tomcat远程调试
- 数据库的命名规范