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. store.js - 轻松实现本地存储(LocalStorage)

    store.js 是一个兼容所有浏览器的 LocalStorage 包装器,不需要借助 Cookie 或者 Flash.store.js 会根据浏览器自动选择使用 localStorage.globa ...

  2. 【position也可以很复杂】当弹出层遇上了鼠标定位(下)

    前言 接着昨天的内容写,为了保证内容连续性,这里还是把昨天的内容拷了过来. 请用现代浏览器测试 引出问题 有图有真相,我们来看一个智联招聘里面经常出现的图层: 他这个是没有什么问题的,我们来简单看看其 ...

  3. 【移动适配】移动Web怎么做屏幕适配(三)

    复杂纷扰的世界背后,总会有万变不离其宗的简单规则 啃先生 Mar.8th.2016 壹 | Fisrt 前面写了两篇移动适配相关的文章: <移动Web怎么做屏幕适配(一)>重点介绍了怎样利 ...

  4. 部署基于国际版Azure的SharePoint三层架构服务器场

    前言 微软Azure国际版已经很普及了,这里没有用国内版(世纪互联),用的是国际版,当然是由于公司性质的缘故.这里一步步图文的方式,分享给大家创建Azure国际版的SharePoint三层架构的过程, ...

  5. Java中“==”与equals方法的区别

    1. 用“==”比较两个变量,如果两个变量是基本类型变量,且都是数值类,则值相等就返回true 如果两个变量是引用型变量,则两个对象的地址一样,即指向同一个对象,则返回true 2.equals:St ...

  6. Safari 快捷键

    标签和网页导航快捷键 8 个 切换到下一个标签页 – Control+Tab 切换到上一个标签页 – Control+Shift+Tab 向下滚动一屏 – 空格 向上滚动一屏 – Shift+空格 焦 ...

  7. array_filter、array_map、array_walk解释

    /** * array_filter 用回调函数处理数组中的各个元素, * 重点在于过滤(而不是新增)某个元素,当你处理到一个元素时, * 如果返回了false,那么这个元素将会被过滤掉.PS:保持了 ...

  8. Android Build Error(1)

    Type 1 —— Build Path Problem : **.jar包文件缺失 1.在Android项目根目录下新建一个libs文件夹: 2.把你需要的导入的第三方Jar包复制进这个目录: 3. ...

  9. 安装SqlServer的时候性能计数器注册表配置单元一致性失败的解决办法

    http://www.2cto.com/database/201204/126772.html

  10. 获取session、request、parmeter的方法

    package com.hanqi.action; import java.util.Map; import com.opensymphony.xwork2.ActionContext; public ...