D. Lakes in Berland
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard 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 nm 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.


题意:有一些湖,定义见原题,填一些湖使得剩下k个湖


DFS找湖

把湖从小到大填就行了

注意递归别调用错函数

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <map>
using namespace std;
typedef long long ll;
const int N=;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int n,m,k;
char g[N][N];
int dx[]={,-,,},dy[]={,,,-};
int vis[N][N],num[N*N],cc=;
struct lakes{
int size,id;
}lake[N*N];
int cnt=;
bool cmp(lakes &a,lakes &b){
return a.size<b.size;
}
void dfs(int x,int y,int id){//printf("dfs %d %d %d\n",x,y,id);
vis[x][y]=id;num[id]++;
for(int i=;i<;i++){
int nx=x+dx[i],ny=y+dy[i];
if(nx<||nx>n||ny<||ny>m) continue;
if(g[nx][ny]=='*'||vis[nx][ny]) continue;
dfs(nx,ny,id);
}
}
int ans=;
void fil(int x,int y,int id){//printf("fil %d %d %d\n",x,y,id);
g[x][y]='*';ans++;
for(int i=;i<;i++){
int nx=x+dx[i],ny=y+dy[i];
if(nx<||nx>n||ny<||ny>m) continue;
if(vis[nx][ny]==id&&g[nx][ny]=='.') fil(nx,ny,id);
}
}
int main(){
n=read();m=read();k=read();
for(int i=;i<=n;i++){
scanf("%s",g[i]);
for(int j=m;j>=;j--) g[i][j]=g[i][j-];
} for(int i=;i<=n;i++){
if(!vis[i][]&&g[i][]=='.') dfs(i,,++cc);
if(!vis[i][m]&&g[i][m]=='.') dfs(i,m,++cc);
}
for(int j=;j<=m;j++){
if(!vis[][j]&&g[][j]=='.') dfs(,j,++cc);
if(!vis[n][j]&&g[n][j]=='.') dfs(n,j,++cc);
}
int sea=cc;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++){
if(!vis[i][j]&&g[i][j]=='.')
dfs(i,j,++cc);
}
for(int i=sea+;i<=cc;i++) {lake[++cnt].size=num[i];lake[cnt].id=i;}//printf("%d %d\n",sea,cc);
sort(lake+,lake++cnt,cmp);
//for(int i=1;i<=cnt;i++) printf("lake %d %d\n",lake[i].id,lake[i].size);
int t=cnt-k,p=;//printf("t %d\n",t);
for(int z=;z<=t;z++){
int fin=;
for(int i=;i<=n;i++){
for(int j=;j<=m;j++)
if(lake[p].id==vis[i][j]){fil(i,j,lake[p++].id);fin=;break;}
if(fin) break;
}
}
printf("%d\n",ans);
for(int i=;i<=n;i++){
for(int j=;j<=m;j++) printf("%c",g[i][j]);
if(i!=n) putchar('\n');
} }

CF723D. Lakes in Berland[DFS floodfill]的更多相关文章

  1. 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 ...

  2. 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 ...

  3. CodeForces 723D Lakes in Berland (dfs搜索)

    题意:给定一个n*m的矩阵,*表示陆地, . 表示水,一些连通的水且不在边界表示湖,让你填最少的陆地使得图中湖剩下恰好为k. 析:很简单的一个搜索题,搜两次,第一次把每个湖的位置和连通块的数量记下来, ...

  4. D. Lakes in Berland (DFS或者BFS +连通块

    https://blog.csdn.net/guhaiteng/article/details/52730373 参考题解 http://codeforces.com/contest/723/prob ...

  5. 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 ...

  6. 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 ...

  7. CF723D 【Lakes in Berland】

    题目链接 题解 CF723D [Lakes in Berland] 首先将边界的水用bfs处理掉 再将中间的每一个湖泊处理出来,存入一个结构体内,结构体里记录湖泊大小和开始点 将湖泊排序从小往大填满, ...

  8. Codeforces Round #375 (Div. 2) D. Lakes in Berland 贪心

    D. Lakes in Berland 题目连接: http://codeforces.com/contest/723/problem/D Description The map of Berland ...

  9. 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 × ...

随机推荐

  1. 高性能javascript学习笔记系列(2)-数据存取

    参考 高性能javascript Tom大叔深入理解javascript系列 相关概念 1.执行上下文   当控制器转到ecmascript可执行代码的时候,就会进入一个执行上下文,执行上下文是以堆栈 ...

  2. AngularJS1.3一些技巧

    前言 框架选择.在上一篇文章评论中,有人说angular1.3是个过时的东西,建议使用angular2.其实这种说法很像拿jQuery1.x和jQuery2.x做比较,新的版本当然会有优化优势的地方, ...

  3. hibernate 入门([数据访问中间件] 开源框架)

    1.内容:  hibernate 也是一个经典的[数据访问中间件] 开源框架.    2.hibernate核心组件       SessionFactory[整个数据的操作]重量级组件       ...

  4. javascript 不响应可能是引用外部javascript时,引用顺序不对。

    有相互引用关系的js,要最后执行的方法所在的js 先被引用. a.js 中有function1 b.js 中有function2 function1 () { function2(){} } 要 &l ...

  5. abap--How to debug backgroud job

    最近被一个朋友问起如何调试后台进程(一个abap的面试题),我一时也不知道如何答,他后来告诉我到sdn上找答案,我现在将答案收集供大家参考:Steps 1. Create variant called ...

  6. AJAX跨域访问(从Tomcat8到Apache/Nginx)

    1.在Tomcat的Root目录下放入如下的文件 apache-tomcat-8.0.12X64\webapps\ROOT clientaccesspolicy.xml文件 <?xml vers ...

  7. 自定义控件设置属性并实时展现并预览在xib中

    关键字: // @IBDesignable:实时看到xib设置后的效果 // @IBInspectable:给xib提供设置属性,可以xib中看到此属性 场景: 自定义一个UITextField,并提 ...

  8. sqlite3 not found问题解决方法

    测试发现,有些Android手机自带sqlite3命令,有些不带.对于不带sqlite3的手机,我们可以手动将sqlite3加入系统. 执行如下命令 adb remount adb push 路径/s ...

  9. iOS不同IDS说明

    1.Vindor标识符  identifierForVendor 一个英文字符串,对于相同的产品商(这里指com.zhang.*,也就是前缀一样),其唯一的标识设备. * 这个值对于相同的产品商在相同 ...

  10. 振奋人心啊!!!!下一代.NET——ASP.NET vNext

    这两天看到的.NET的新闻都好振奋人心啊!微软北美技术大会带来了好多好消息! 看到一篇博客园的文章,感觉太棒了.摘录下来.原文链接:http://news.cnblogs.com/n/208133/ ...