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 ...
随机推荐
- FineReport中如何进行Informix数据库连接
报表开发工具Finereport中,对于Informix数据库,定义数据连接处进行如下配置: 数据库:Others 驱动器:com.informix.jdbc.IfxDriver URL: jdbc: ...
- 《The Elder Scrolls V: Skyrim》百般冷门却强力职业
<The Elder Scrolls V: Skyrim>百般冷门却强力职业 1.有如成龙平常的杂耍型战斗窃贼 每次看帖都察觉大伙一贯在强调窃贼不需要防御,窃贼不需要血,窃贼就是一击致命, ...
- hdu-5977 Garden of Eden(树分治)
题目链接: Garden of Eden Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/ ...
- 洛谷U4859matrix[单调栈]
题目描述 给一个元素均为正整数的矩阵,上升矩阵的定义为矩阵中每行.每列都是严格递增的. 求给定矩阵中上升子矩阵的数量. 输入输出格式 输入格式: 第一行两个正整数n.m,表示矩阵的行数.列数. 接下来 ...
- Java里String.split需要注意的用法
我们常常用String的split()方法去分割字符串,有两个地方值得注意: 1. 当分隔符是句号时("."),需要转义: 由于String.split是基于正则表达式来分割字符串 ...
- Js 遍历json对象所有key及根据动态key获取值
var obj = {}; for(var k in obj) { //遍历对象,k即为key,obj[k]为当前k对应的值 console.log(obj[k]); } 文章来自:https://z ...
- Js前端代码异常监控
window.onerror = function(msg,url,line,col,error){ //没有URL不上报!上报也不知道错误 if (msg != "Script error ...
- Vmware虚拟机配置LVS/NAT模式遇到的坑。
这两天在研究LVS的负载均衡,先从最简单的LVS/NAT模式开始入手. 最后配置完之后能够相互之间Ping通,并且能够直接访问real服务器提供的web服务,而且防火墙也已经关闭了. 但是通过访问LV ...
- JavaScript 特殊对象 Array-Like Objects 详解
这篇文章拖了有两周,今天来跟大家聊聊 JavaScript 中一类特殊的对象 -> Array-Like Objects. (本文节选自 underscore 源码解读系列文章,完整版请关注 h ...
- android 动画
public void onClicked(View v_) { //wa.startAnimation(); // TextView tv = (TextView)findViewById(R.id ...