博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CF723D. Lakes in Berland[DFS floodfill]
阅读量:5244 次
发布时间:2019-06-14

本文共 4215 字,大约阅读时间需要 14 分钟。

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

 

转载于:https://www.cnblogs.com/candy99/p/5930708.html

你可能感兴趣的文章
win7 wifi
查看>>
java gc的工作原理、如何优化GC的性能、如何和GC进行有效的交互
查看>>
今年剩余几个月的大概计划安排
查看>>
CSS3魔法堂:说说Multi-column Layout
查看>>
关于调整浏览器窗口JS
查看>>
J2EE 全面简介
查看>>
get post
查看>>
Requests+正则表达式抓取猫眼电影TOP100
查看>>
PCA主成分分析 ICA独立成分分析 LDA线性判别分析 SVD性质
查看>>
bzoj3223: Tyvj 1729 文艺平衡树 splay裸题
查看>>
URAL 1019 Line Painting
查看>>
HDU 3647 Tetris
查看>>
迭代器
查看>>
Python文件操作汇总
查看>>
[环境]Anaconda 镜像使用帮助
查看>>
shell脚本备份日志
查看>>
《Unity_API解析》 第十章 Rigidbody类
查看>>
Git简介及安装和简单配置
查看>>
10 noip 引水入城 解题报告
查看>>
ie6 背景边框缺失 bug
查看>>