F - Lakes in Berland(BFS)
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 klakes 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.
Example
5 4 1
****
*..*
****
**.*
..**
1
****
*..*
****
****
..**
3 3 0
***
*.*
***
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.
1 #include <iostream>
2 #include <cstdio>
3 #include <algorithm>
4 #include <cstring>
5 #include <map>
6 using namespace std;
7 typedef long long ll;
8 const int N=55;
9 inline int read(){
10 char c=getchar();int x=0,f=1;
11 while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
12 while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
13 return x*f;
14 }
15 int n,m,k;
16 char g[N][N];
17 int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1};
18 int vis[N][N],num[N*N],cc=0;
19 struct lakes{
20 int size,id;
21 }lake[N*N];
22 int cnt=0;
23 bool cmp(lakes &a,lakes &b){
24 return a.size<b.size;
25 }
26 void dfs(int x,int y,int id){//printf("dfs %d %d %d\n",x,y,id);
27 vis[x][y]=id;num[id]++;
28 for(int i=0;i<4;i++){
29 int nx=x+dx[i],ny=y+dy[i];
30 if(nx<1||nx>n||ny<1||ny>m) continue;
31 if(g[nx][ny]=='*'||vis[nx][ny]) continue;
32 dfs(nx,ny,id);
33 }
34 }
35 int ans=0;
36 void fil(int x,int y,int id){//printf("fil %d %d %d\n",x,y,id);
37 g[x][y]='*';ans++;
38 for(int i=0;i<4;i++){
39 int nx=x+dx[i],ny=y+dy[i];
40 if(nx<1||nx>n||ny<1||ny>m) continue;
41 if(vis[nx][ny]==id&&g[nx][ny]=='.') fil(nx,ny,id);
42 }
43 }
44 int main(){
45 n=read();m=read();k=read();
46 for(int i=1;i<=n;i++){
47 scanf("%s",g[i]);
48 for(int j=m;j>=1;j--) g[i][j]=g[i][j-1];
49 }
50
51 for(int i=1;i<=n;i++){
52 if(!vis[i][1]&&g[i][1]=='.') dfs(i,1,++cc);
53 if(!vis[i][m]&&g[i][m]=='.') dfs(i,m,++cc);
54 }
55 for(int j=1;j<=m;j++){
56 if(!vis[1][j]&&g[1][j]=='.') dfs(1,j,++cc);
57 if(!vis[n][j]&&g[n][j]=='.') dfs(n,j,++cc);
58 }
59 int sea=cc;
60 for(int i=1;i<=n;i++)
61 for(int j=1;j<=m;j++){
62 if(!vis[i][j]&&g[i][j]=='.')
63 dfs(i,j,++cc);
64 }
65 for(int i=sea+1;i<=cc;i++) {lake[++cnt].size=num[i];lake[cnt].id=i;}//printf("%d %d\n",sea,cc);
66 sort(lake+1,lake+1+cnt,cmp);
67 //for(int i=1;i<=cnt;i++) printf("lake %d %d\n",lake[i].id,lake[i].size);
68 int t=cnt-k,p=1;//printf("t %d\n",t);
69 for(int z=1;z<=t;z++){
70 int fin=0;
71 for(int i=1;i<=n;i++){
72 for(int j=1;j<=m;j++)
73 if(lake[p].id==vis[i][j]){fil(i,j,lake[p++].id);fin=1;break;}
74 if(fin) break;
75 }
76 }
77 printf("%d\n",ans);
78 for(int i=1;i<=n;i++){
79 for(int j=1;j<=m;j++) printf("%c",g[i][j]);
80 if(i!=n) putchar('\n');
81 }
82
83 }
F - Lakes in Berland(BFS)的更多相关文章
- 【29.70%】【codeforces 723D】Lakes in Berland
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- codeforce375div2-D. Lakes in Berland 搜索
Lakes in Berland 题意与解释:这道题就是求图中被围起来的点群,问最少去掉几个点,可以使得孤立的点群数目为K; 因为自己写的代码又长又had bugs. 我自己写的bfs,想着是先染色, ...
- CF723D. Lakes in Berland[DFS floodfill]
D. Lakes in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- codeforces 723D: Lakes in Berland
Description The map of Berland is a rectangle of the size n × m, which consists of cells of size 1 × ...
- CF723D 【Lakes in Berland】
题目链接 题解 CF723D [Lakes in Berland] 首先将边界的水用bfs处理掉 再将中间的每一个湖泊处理出来,存入一个结构体内,结构体里记录湖泊大小和开始点 将湖泊排序从小往大填满, ...
- cf723d Lakes in Berland
The map of Berland is a rectangle of the size n × m, which consists of cells of size 1 × 1. Each cel ...
- Codeforces Round #375 (Div. 2)——D. Lakes in Berland(DFS连通块)
D. Lakes in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Round #375 (Div. 2) D. Lakes in Berland dfs
D. Lakes in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- codeforces723 D. Lakes in Berland(并查集)
题目链接:codeforces723 D. Lakes in Berland 参考博客:http://www.cnblogs.com/Geek-xiyang/p/5930245.html #inclu ...
随机推荐
- SpringBoot(八):SpringBoot中配置字符编码 Springboot中文乱码处理
SpringBoot中配置字符编码一共有两种方式 方式一: 使用传统的Spring提供的字符编码过滤器(和第二种比较,此方式复杂,由于时间原因这里先不介绍了,后续补上) 方式二(推荐使用) 在appl ...
- C# ref and out
相同点: 1. ref 和 out 都是按地址传递的,使用后都将改变原来参数的数值: 2. 方法定义和调用方法都必须显式使用 ref 或者 out关键字: 3. 通过ref 和 ref 特性,一定程度 ...
- pytorch(14)权值初始化
权值的方差过大导致梯度爆炸的原因 方差一致性原则分析Xavier方法与Kaiming初始化方法 饱和激活函数tanh,非饱和激活函数relu pytorch提供的十种初始化方法 梯度消失与爆炸 \[H ...
- 【粉丝问答10】C语言关键字static的使用详解
视频地址:https://www.ixigua.com/6935761378816819748 粉丝提问 粉丝问题,总结一下: 关键字static的使用方法. 要想搞清楚关键字static的使用方法, ...
- 翻译:《实用的Python编程》04_02_Inheritance
目录 | 上一节 (4.1 类) | 下一节 (4.3 特殊方法) 4.2 继承 继承(inheritance)是编写可扩展程序程序的常用手段.本节对继承的思想(idea)进行探讨. 简介 继承用于特 ...
- 性能追击:万字长文30+图揭秘8大主流服务器程序线程模型 | Node.js,Apache,Nginx,Netty,Redis,Tomcat,MySQL,Zuul
本文为<高性能网络编程游记>的第六篇"性能追击:万字长文30+图揭秘8大主流服务器程序线程模型". 最近拍的照片比较少,不知道配什么图好,于是自己画了一个,凑合着用,让 ...
- LAB1 启动操作系统
从机器上电到运行OS发生了什么? 在电脑主板上有一个Flash块,存放了BIOS的可执行代码.它是ROM,断电不会丢掉数据.在机器上电的时候,CPU要求内存控制器从0地址读取数据(程序第一条指令)的时 ...
- python爬虫加定时任务,制作微信提醒备忘录
一.任务的记录与提取 1.1 制作每日任务 为了便于爬取,推荐使用网页版的在线记事本,现在这种工具很多,我选择"石墨文档"进行操作演示.记录内容的 格式可以根据自己的需求和爬虫自行 ...
- P1012 拼数(JAVA语言)
//早起刷题傻一天 题目描述 设有nn个正整数(n≤20)(n≤20),将它们联接成一排,组成一个最大的多位整数. 例如:n=3n=3时,33个整数1313,312312,343343联接成的最大整数 ...
- P1579_哥德巴赫猜想(JAVA语言)
题目背景 1742年6月7日哥德巴赫写信给当时的大数学家欧拉,正式提出了以下的猜想:任何一个大于9的奇数都可以表示成3个质数之和.质数是指除了1和本身之外没有其他约数的数,如2和11都是质数,而6不是 ...