D. Lakes in Berland
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
The map of Berland is a rectangle of the size n × m, which consists of cells of size 1 × 1. Each cell is either land or water. The map is surrounded by the ocean.

Lakes are the maximal regions of water cells, connected by sides, which are not connected with the ocean. Formally, lake is a set of water cells, such that it’s possible to get from any cell of the set to any other without leaving the set and moving only to cells adjacent by the side, none of them is located on the border of the rectangle, and it’s impossible to add one more water cell to the set such that it will be connected with any other cell.

You task is to fill up with the earth the minimum number of water cells so that there will be exactly k lakes in Berland. Note that the initial number of lakes on the map is not less than k.

Input
The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 50, 0 ≤ k ≤ 50) — the sizes of the map and the number of lakes which should be left on the map.

The next n lines contain m characters each — the description of the map. Each of the characters is either ‘.’ (it means that the corresponding cell is water) or ‘*’ (it means that the corresponding cell is land).

It is guaranteed that the map contain at least k lakes.

Output
In the first line print the minimum number of cells which should be transformed from water to land.

In the next n lines print m symbols — the map after the changes. The format must strictly follow the format of the map in the input data (there is no need to print the size of the map). If there are several answers, print any of them.

It is guaranteed that the answer exists on the given data.

Examples
input
5 4 1


..


*.
..**
output
1


..



..**
input
3 3 0


.


output
1




Note
In the first example there are only two lakes — the first consists of the cells (2, 2) and (2, 3), the second consists of the cell (4, 3). It is profitable to cover the second lake because it is smaller. Pay attention that the area of water in the lower left corner is not a lake because this area share a border with the ocean.

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;
struct node{int cost, x, y;node(int cost0, int x0, int y0):cost(cost0), x(x0), y(y0){}node():cost(0), x(0), y(0){}bool operator<(const node& a)const{return cost<a.cost;
}
}block[100000];
int cnt;
int n,m,k;
char mapp[55][55];
bool vis[555][555];int dr[4]={0, -1, 0, 1};
int dc[4]={-1, 0, 1, 0};
bool flag;
void bfs(int r, int c){if (r==0 ||r==n-1||c==0||c==m-1)flag=false;cnt++;//cout<<"in bfs "<<r<<" "<<c<<endl;for (int i=0; i<4; i++){int nr=r+dr[i]; int nc=c+dc[i];if (nr>=n || nr<0 || nc>=m || nc<0){continue;}else{if (!vis[nr][nc] && mapp[nr][nc]=='.'){vis[nr][nc]=1;bfs(nr, nc);}}}
}void bfs2(int r, int c){cnt++;for (int i=0; i<4; i++){int nr=r+dr[i]; int nc=c+dc[i];if (nr>=n || nr<0 || nc>=m || nc<0){continue;}else{if (mapp[nr][nc]=='.'){//cout<<"nr nc="<<nr<<" "<<nc<<endl;mapp[nr][nc]='*';bfs2(nr, nc);}}}
}
int main(){vector<node> vec;scanf("%d%d%d", &n, &m, &k);vec.clear();//vec.resize(1);for (int i=0; i<n; i++){scanf("%s", mapp[i]);}memset(vis, 0, sizeof(vis));int cct=0;for (int i=0; i<n; i++){for (int j=0; j<m; j++){if (!vis[i][j] && mapp[i][j]=='.'){//cout<<"for"<<i<<j<<endl;//cout<<"vis"<<vis[i][j]<<endl;vis[i][j]=1;cnt=0;flag=true;bfs(i, j);//cout<<"cnt="<<cnt<<endl;if (flag){vec.push_back(node(cnt, i, j));cct++;}}}}//cout<<"size="<<vec.size()<<endl;sort(vec.begin(), vec.begin()+vec.size());int res=0;for (int i=0; i<vec.size()-k; i++){res+=vec[i].cost;mapp[vec[i].x][vec[i].y]='*';bfs2(vec[i].x, vec[i].y);}cout<<res<<endl;for (int i=0; i<n; i++){cout<<mapp[i]<<endl;}return 0;
}

cf723D 连通块相关推荐

  1. [C] [编程题]连通块(DFS解决)

    时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 256M,其他语言512M 来源:牛客网 金山办公2020校招服务端开发工程师笔试题(一) 题目描述 给一个01矩阵,1代表是陆地,0代表 ...

  2. [C] 深度优先搜索解决连通块/染色问题——求岛的个数

    本文介绍用DFS解决连通块个数问题 有关dfs的介绍见另外一篇:不撞南墙不回头--深度优先搜索 例题 宝岛探险 题目描述 一个小岛由一个主岛和一些复附属岛屿组成,该岛使用一个二维矩阵表示,其中数字表示 ...

  3. C. Edgy Trees Codeforces Round #548 (Div. 2) 【连通块】

    一.题面 here 二.分析 这题刚开始没读懂题意,后来明白了,原来就是一个数连通块里点数的问题.首先在建图的时候,只考虑红色路径上的点.为什么呢,因为为了不走红色的快,那么我们可以反着想只走红色的路 ...

  4. 树形dp ---- gym101655 D - Delta Quadrant 树上连通块思维换根 + 树形dp

    题目链接 题目大意: 给你一颗NNN个节点的树,树上每条边有边权就是遍历的时间,你可以从任意节点出发遍历N−kN-kN−k个点并且回到出发点,问你最短的时间是多少? k∈[0,min(N,20)],N ...

  5. UVa572 Oil Deposits DFS求连通块

    技巧:遍历8个方向 for(int dr = -1; dr <= 1; dr++)for(int dc = -1; dc <= 1; dc++)if(dr != 0 || dc != 0) ...

  6. DFS求连通块数目(深搜)

    DFS求连通块数目 这里认为,连通块是包括斜对角线的路径连通的块. 测试数据 5 5 ****@ *@@*@ *@**@ @@@*@ @@**@ 计算通过@相连的连通块的个数 测试输出: 2 样例代码 ...

  7. 求连通块个数(使用并查集)

    并查集求连通块个数的模板 #include<bits/stdc++.h>using namespace std;const int maxn = 1e5+5; vector<int& ...

  8. 1021 Deepest Root (25 分) 【难度: 中 / 知识点: 树的直径 连通块】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805482919673856 方法一: 数组模拟邻接表 第一步: 爆搜df ...

  9. 1013 Battle Over Cities (25 分) 【难度: 中 / 知识点: 连通块】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805500414115840 将这些连通块,连接起来最少的边,即是答案. ...

最新文章

  1. 业内大牛送给计算机方向学生的7个实用建议!
  2. 在运行期通过反射了解JVM内部机制
  3. 统计信号处理_声学前端:深度学习算法和传统信号处理方法各有千秋
  4. leftjoin多个on条件_MYSQL|为什么LEFT JOIN会这么慢?
  5. Javascript模块模式学习分享
  6. 如何将Pcm格式的音频文件转换成Wave格式的文件
  7. 把准脉搏 U-Mail邮件系统2014开足马力
  8. 彻底理解内存泄漏,memory leak
  9. 内大计算机学院研究生奖学金,通知 | 【研究生评奖评优】关于做好浙江大学2017-2018学年计算机学院研究生学年小结及评奖评优工作的通知...
  10. MyBatis查询返回类型为int,查询结果为空NULL,报异常解决
  11. mysql 安装参考
  12. eve-ng:加载c7200 dynamips镜像
  13. logisim软件使用学习
  14. 亿图图示+linux版本,亿图图示linux版下载
  15. 解决局域网文件共享“****无法复制,指定的网络名不可用”
  16. 计算机接入因特网有几种方式有哪些,简述几种因特网的接入方式?
  17. 招聘简历管理系统的简单设计
  18. 夜深人静写算法(三十二)- 费马小定理
  19. screen:There is no screen to be resumed matching XXX 解决办法
  20. 互联网企业使用云计算,有什么优势?

热门文章

  1. dropzone.js php,上传30秒后,使用PHP上传的Dropzone.js失败
  2. 小码哥-斗鱼直播APP之游戏界面实现
  3. 爱--生活--名称解释
  4. Windows Embedded从入门到精通6月预告
  5. vscode +phpstudy 搭建php调试环境
  6. Android WiFi开发教程
  7. 集成灶哪个品牌好一点,10分钟学会挑选集成灶不踩雷
  8. ThreadLocal全面解析
  9. Reports Builder 数据源(文本和XML导入)
  10. VBA 函数参数和结果输出的二三事