CF723D. Lakes in Berland[DFS floodfill]
2 seconds
256 megabytes
standard input
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.
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.
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.
5 4 1
****
*..*
****
**.*
..**
1
****
*..*
****
****
..**
3 3 0
***
*.*
***
1
***
***
***
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]的更多相关文章
- 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 723D Lakes in Berland (dfs搜索)
题意:给定一个n*m的矩阵,*表示陆地, . 表示水,一些连通的水且不在边界表示湖,让你填最少的陆地使得图中湖剩下恰好为k. 析:很简单的一个搜索题,搜两次,第一次把每个湖的位置和连通块的数量记下来, ...
- D. Lakes in Berland (DFS或者BFS +连通块
https://blog.csdn.net/guhaiteng/article/details/52730373 参考题解 http://codeforces.com/contest/723/prob ...
- 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 ...
- CF723D 【Lakes in Berland】
题目链接 题解 CF723D [Lakes in Berland] 首先将边界的水用bfs处理掉 再将中间的每一个湖泊处理出来,存入一个结构体内,结构体里记录湖泊大小和开始点 将湖泊排序从小往大填满, ...
- 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 ...
- 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 × ...
随机推荐
- CSS代码记录
1. 内容横向滚动的代码 .ul { display: box; display: -webkit-box; width: 250px; background: yellow; overflow-y: ...
- 定制Eclipse IDE之功能篇(二)
上文回顾:定制Eclipse IDE之功能篇(一) 这一篇文章将记录一些Eclipse插件小功能,Smart but Useful. 一.设置工作空间 文本文件的编码 解决办法: 在org ...
- Flex Viewer(三)——Config的原理
一.概述 在上文<深入浅出Flex Viewer(二)——体系结构>中,笔者详细介绍了到Flex Viewer框架,使得读者能够对该框架源代码的关键目录和文件结构和这些文件中所包含或涉及到 ...
- SAP 应用服务负载均衡的实现
共两步,一是服务器的设置,二是客户端登陆设置. 先在SAP中使用SMLG 进行服务器分组.实例名是SAP系统中定义过的,你没法删也没改.(可能是俺不会,会的教教).我们先建一个Gro ...
- 一维Poisson方程计算
package com.smartmap.algorithm.equation.differential.partial.ellipsoidal; import java.io.FileOutputS ...
- Sharepoint学习笔记—习题系列--70-576习题解析 -(Q36-Q39)
Question 36 You are designing a SharePoint 2010 application. You need to design the application so t ...
- Android自带的theme
android:theme="@android:style/Theme.Dialog" 将一个Activity显示为能话框模式 android:theme="@andro ...
- '[<NSObject 0x8a4b500> setValue:forUndefinedKey:]
Bug如下: Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUnd ...
- Mac 常用快捷键
Command+Tab 任意情况下切换应用程序 - 向前循环 Shift+Command+Tab 切换应用程序 - 向后循环 Command+Delete 把选中的资源移到废纸篓 Shift+Comm ...
- C语言中qsort函数的应用
qsort函数包含在<stdlib.h>的头文件里,本文中排序都是采用的从小到大排序 一.对int类型数组排序 ]; int cmp ( const void *a , const voi ...