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 ...
随机推荐
- java设计模式之观察者模式
观察者模式 观察者模式(有时又被称为发布(publish )-订阅(Subscribe)模式.模型-视图(View)模式.源-收听者(Listener)模式或从属者模式)是软件设计模式的一种.在此种模 ...
- 【2016-10-28】【坚持学习】【Day15】【MongoDB】【初识】
其实公司产品一直有使用mongodb,只不过我一直没有接触这一块,也没有主动的了解.实在说不过去.于是,准备写几个文章,认真学习一下它. 今天花了几个小时学习了入门 定义: 非关系型数据库, NoSQ ...
- 不要让catalogs搞死你的eclipse
不要让catalogs搞死你的eclipse 楼主终于走出校园,踏上软件职场之路.新人要学的东西有很多,要战胜的东西很多,这些东西除了技术本身,还有对未知领域的恐惧,新接触技术,比如新的框架,可能好几 ...
- WinCE项目应用汇总
虚拟仪器(VI) RM905a+医用放射性核素活度计 RM905a+活度计远程检定方法研究 车载导航
- POJ1112 Team Them Up![二分图染色 补图 01背包]
Team Them Up! Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7608 Accepted: 2041 S ...
- 解析ThreadLocal
如果定义了一个单实例的java bean,它有若干属性,但是有一个属性不是线程安全的,比如说HashMap.并且碰巧你并不需要在不同的线程中共享这个属性,也就是说这个属性不存在跨线程的意义.那么不推荐 ...
- Spring知识点提炼
原文出处: 朱小厮 1. Spring框架的作用 轻量:Spring是轻量级的,基本的版本大小为2MB 控制反转:Spring通过控制反转实现了松散耦合,对象们给出它们的依赖,而不是创建或查找依赖的对 ...
- Yeelink 初探
Yeelink可以作为中转服务器使用,在自己没有服务器的情况下,可以利用它传输自己的数据. 首先去申请一个帐号,然后添加一个设备. http://www.yeelink.net/user 在这里是用户 ...
- 积极主动敲代码,使用Junit学习Java程序设计
积极主动敲代码,使用JUnit学习Java 早起看到周筠老师在知乎的回答软件专业成绩很好但是实际能力很差怎么办?,很有感触. 从读大学算起,我敲过不下100本程序设计图书的代码,我的学习经验带来我的程 ...
- JS组件系列——分享自己封装的Bootstrap树形组件:jqTree
前言:之前的一篇介绍了下如何封装自己的组件,这篇再次来体验下自己封装组件的乐趣.看过博主博客的园友应该记得之前分享过一篇树形菜单的使用JS组件系列——Bootstrap 树控件使用经验分享,这篇里面第 ...