CF723D. Lakes in Berland[DFS floodfill]

时间:2022-09-13 12:34:39
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 &num;375 &lpar;Div&period; 2&rpar; D&period; 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 &lpar;dfs搜索&rpar;

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

  4. D&period; Lakes in Berland &lpar;DFS或者BFS &plus;连通块

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

  5. Codeforces Round &num;375 &lpar;Div&period; 2&rpar;——D&period; 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 &num;375 &lpar;Div&period; 2&rpar; D&period; 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 &num;375 &lpar;Div&period; 2&rpar; D&period; 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. HTML5自定义属性之data-&ast;

    HTML5 增加了一项新功能是 自定义数据属性 ,也就是  data-* 自定义属性.在HTML5中我们可以使用以 data- 为前缀来设置我们需要的自定义属性,来进行一些数据的存放.当然高级浏览器下 ...

  2. Python检查xpath和csspath表达式是否合法

    在做一个可视化配置爬虫项目时,需要配置爬虫的用户自己输入xpath和csspath路径以提取数据或做浏览器操作.考虑到用户的有时会输入错误的xpath或csspath路径,后台需要对其做合法性校验. ...

  3. 如何生成ipa文件

    xcode--Product--Archive 弹出Organizer-Archives选择Distribute---Save for EnterPrise or Ad_Hoc Deployment- ...

  4. VS的基本学习

    2016.4.11  下午 一.数据类型 1.基本数据类型 注:字节:例{10221021  8位数为一个字节    8b=1B} 1).整形(整数) ① short(比Int短   Int16){2 ...

  5. c位段

    假如程序表示四盏灯的开关状态灯只有开或关两种状态所以用1和0就可以表示为了节省内存就用一个二进制位表示一盏灯这里就定义位域用 a b c d 各表示一盏 这里定义时注意选用无符号类型位域允许用各种格式 ...

  6. 用VirtualBox构建MySQL测试环境笔记

    网络环境: 宿主机:Win7 VirtualBox 4.1.4 + Ubuntu 11.10 server 64bit 宿主机使用网线的时候,客户机在Bridged Adapter模式下,使用Athe ...

  7. Android推送通知Notification

    参考: 1.http://www.cnblogs.com/anrainie/archive/2012/03/07/2383941.html 总结: 代码:

  8. mybatis从数据库中取到的date格式不是yyyy-MM-dd HH&colon;mm&colon;ss

    问题:sqlserver中的存储时间格式为date,pojo的时间属性也是date,直接mybatis取出的时间格式是带英语的那种,不满足客户要求. 解决:将pojo的时间属性改为string类型,在 ...

  9. Object类的方法

    一 Object:类Object是类层次结构的跟类,每个类都使用Object作为超类,每个类都是直接或者间接的继承自Object类. Object类的方法: ①public int hashCode( ...

  10. 配置percona mysql server 5&period;7基于gtid主主复制架构

    配置mysql基于gtid主主复制架构 环境: 操作系统 centos7. x86_64 mysql版本:Percona-Server-- 测试环境: node1 10.11.0.210 node2 ...