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 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.
/*
不碰边界的连通块算是一个湖,给你要保留的湖的数目,一次填一个点,求填湖的最少次数
sb爆搜,注意最后那个贪心选湖,还有那个边界的判断,比赛写错了
*/
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
struct orz{
int p;
int sz;
};
orz lke[];
int mp[][],vis[][],cnt,n,m,k,tot;
bool ok[],sgn[];
int dx[] = {-,,,};
int dy[] = {,,,-};
char cmd;
bool jud(int y,int x){
return y >= && x >= && y <= n && x <= m && !vis[y][x] && mp[y][x] == ;
}
bool dfs(int y,int x){
vis[y][x] = cnt;
lke[cnt].sz++;
int ny,nx;
int gg = false;
for(int i = ;i < ;i++){
ny = y + dy[i];
nx = x + dx[i];
if(ny < || ny > n || nx < || nx > m) gg = true;
if(jud(ny,nx)) if(!dfs(ny,nx)) gg = true;
}
if(gg) return false;
else return true;
}
bool cmp(orz a,orz b){
return a.sz < b.sz;
}
int main(){
cin>>n>>m>>k;
for(int i = ;i <= n;i++){
for(int j = ;j <= m;j++){
scanf("%c",&cmd);
while(cmd != '*' && cmd != '.') scanf("%c",&cmd);
if(cmd == '*') mp[i][j] = ;
else if(cmd == '.') mp[i][j] = ;
}
}
for(int i = ;i <= n;i++){
for(int j = ;j <= m;j++){
if(mp[i][j] == && !vis[i][j]){
cnt++;
lke[cnt].p = cnt;
ok[cnt] = dfs(i,j);
if(ok[cnt]) tot++;
}
}
}
sort(lke+,lke++cnt,cmp);
int chs = ,ans = ;
for(int i = ;i <= cnt;i++){
if(chs >= tot - k) break;
if(ok[lke[i].p]){
sgn[lke[i].p] = true;
chs++;
ans += lke[i].sz;
} }
cout<<ans<<endl;
for(int i = ;i <= n;i++){
for(int j = ;j <= m;j++){
if(mp[i][j] == || sgn[vis[i][j]]) cout<<"*";
else cout<<".";
}
cout<<endl;
}
return ;
}
cf723d Lakes in Berland的更多相关文章
- CF723D. Lakes in Berland[DFS floodfill]
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 723D: Lakes in Berland
Description The map of Berland is a rectangle of the size n × m, which consists of cells of size 1 × ...
- 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 ...
- codeforces723 D. Lakes in Berland(并查集)
题目链接:codeforces723 D. Lakes in Berland 参考博客:http://www.cnblogs.com/Geek-xiyang/p/5930245.html #inclu ...
- 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 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 ...
- 【29.70%】【codeforces 723D】Lakes in Berland
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
随机推荐
- Apache2.4部署django出现403 Forbidden错误解决办法
前言:Apache2.4部署django出现403 Forbidden错误最好要结合apache中的错误日志来观察出现何种错误导致出现403错误 下午百度了一下午没找到解决办法,试了n种方法,简直坑爹 ...
- [WPF系列]-DataBinding 绑定计算表达式
Width="{Binding RelativeSource={RelativeSource Self}, Path=ActualWidth, Converter={Stat ...
- QuickHit项目(输出字符串游戏)
public class leve { private int leveNo; private int strLength; private int strTimes; private int tim ...
- POJ1274 The Perfect Stall[二分图最大匹配]
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 23911 Accepted: 106 ...
- python BeautifulSoup模块的简要介绍
常用介绍: pip install beautifulsoup4 # 安装模块 from bs4 import BeautifulSoup # 导入模块 soup = BeautifulSoup(ht ...
- 破解Java to C# Converter
起因 最近在对接一个第三方平台.该平台只提供了Java版本的SDK,C#版本的还处于敬请期待状态.由于C#可以复用绝大部分代码,便考虑找一个Java到C#的转换器,在试用了几个软件之后,发现还是Jav ...
- iOS 2D绘图 (Quartz2D)之路径(点,直线,虚线,曲线,圆弧,椭圆,矩形)
博客原地址:http://blog.csdn.net/hello_hwc?viewmode=list 让我们继续跟着大神的脚步前进吧.这一次 我们学习一些Quartz 2D 最基本的一些用法. 前言: ...
- js中的按键事件
参考链接:http://blog.csdn.net/zhouziyu2011/article/details/53978293 <input type="text" id=& ...
- Lrc2srt精灵,增加自定义输出编码
2015.4.8 对中文支持有点问题,修改了一下,支持自定义输出编码! 修改了建议行末偏移,通常100到200最好了,人的反应时间! http://files.cnblogs.com/files/ro ...
- 以libfuse为例介绍rpm打包工具rpmbuild的使用和SPEC文件的编写
一.rpmbuild命令的安装 yum install rpm-build 二.用法 rpmbuild -bb XXXX.spec或者rpmbuild -ba XXX.tar.gz 三.目录概述 rp ...