Codeforces Round #375 (Div. 2) D. Lakes in Berland dfs
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.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
const int N=2e5+,M=4e6+,inf=1e9+,mod=1e9+;
const ll INF=1e18+;
int n,m,k;
char mp[][];
int xx[]={,,-,};
int yy[]={,-,,};
int vis[][];
int p[N];
struct is
{
int si;
int pos;
/*bool operator <(const is &b)const
{
if(p[pos]!=p[b.pos])
return p[pos]<p[b.pos];
return si>b.si;
}*/
}a[N];
int cmp(is a,is b)
{
if(p[a.pos]!=p[b.pos])
return p[a.pos]<p[b.pos];
return a.si>b.si;
}
int check(int x,int y)
{
if(x>=n||x<||y>=m||y<) return ;
return ;
}
void dfs(int x,int y,int z)
{
vis[x][y]=z;
for(int i=;i<;i++)
{
int xxx=x+xx[i];
int yyy=y+yy[i];
if(check(xxx,yyy)&&mp[xxx][yyy]=='.'&&!vis[xxx][yyy])
{
dfs(xxx,yyy,z);
}
}
}
int main()
{
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<n;i++)
scanf("%s",mp[i]);
int flag=;
for(int i=;i<n;i++)
{
for(int t=;t<m;t++)
{
if(mp[i][t]=='.'&&!vis[i][t])
dfs(i,t,flag++);
}
}
for(int i=;i<flag;i++)
a[i].pos=i,a[i].si=;
for(int i=;i<n;i++)
{
for(int t=;t<m;t++)
{
if(vis[i][t]>)
{
a[vis[i][t]].si++;
if(i==||i==n-||t==||t==m-)
p[vis[i][t]]=;
}
}
}
sort(a+,a+flag,cmp);
int ans=;
for(int i=;i<flag;i++)
{
if(p[a[i].pos]==&&i>k)
ans+=a[i].si;
}
for(int i=;i<=k;i++)
p[a[i].pos]=;
printf("%d\n",ans);
for(int i=;i<n;i++)
{
for(int t=;t<m;t++)
{
if(vis[i][t]>)
{
if(p[vis[i][t]])
printf(".");
else
printf("*");
}
else
printf("*");
}
printf("\n");
}
return ;
}
Codeforces Round #375 (Div. 2) D. Lakes in Berland dfs的更多相关文章
- 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 ...
- 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 并查集
http://codeforces.com/contest/723/problem/D 这题是只能把小河填了,题目那里有写,其实如果读懂题这题是挺简单的,预处理出每一块的大小,排好序,从小到大填就行了 ...
- Codeforces Round #375 (Div. 2)
A. The New Year: Meeting Friends 水 #include <set> #include <map> #include <stack> ...
- Codeforces Round #375 (Div. 2) ABCDE
A - The New Year: Meeting Friends 水 #include<iostream> #include<algorithm> using namespa ...
- Codeforces Round #375 (Div. 2) - D
题目链接:http://codeforces.com/contest/723/problem/D 题意:给定n*m小大的字符矩阵.'*'表示陆地,'.'表示水域.然后湖的定义是:如果水域完全被陆地包围 ...
- Codeforces Round #375 (Div. 2) - C
题目链接:http://codeforces.com/contest/723/problem/C 题意:给定长度为n的一个序列.还有一个m.现在可以改变序列的一些数.使得序列里面数字[1,m]出现次数 ...
- Codeforces Round #375 (Div. 2) - B
题目链接:http://codeforces.com/contest/723/problem/B 题意:给定一个字符串.只包含_,大小写字母,左右括号(保证不会出现括号里面套括号的情况),_分隔开单词 ...
随机推荐
- PHP判断客户端是PC web端还是移动手机端方法
PHP判断客户端是PC web端还是移动手机端方法需要实现:判断手机版的内容加上!c550x260.jpg后缀变成缩略图PHP用正则批量替换Img中src内容,用正则表达式获取图片路径实现缩略图功能 ...
- 在使用Myeclipse时,用Tomcat添加部署项目的时候报错,或启动tomcat报错
the selected server is enabled,but is not configured properly.deployment to it will not be permitted ...
- Linux下创建与解压tar, tar.gz和tar.bz2文件及压缩率对比 | 沉思小屋
刚 在qq群里面一位仁兄问到文件压缩的命令,平时工作中大多用解压缩命令,要是遇到压缩就现查(这不是一个好习惯),于是整理下Linux下创建与解压 zip.tar.tar.gz和tar.bz2文件及他们 ...
- Oracle游标练手实例
--声明游标:CURSOR cursor_name IS select_statement --For循环游标 --(1)定义游标 --(2)定义游标变量 --(3)使用for循环来使用这个游标 de ...
- 20145227 《Java程序设计》实验五实验报告
20145227 <Java程序设计>实验五实验报告 实验内容 1.掌握Socket程序的编写: 2.掌握密码技术的使用: 3.设计安全传输系统. 实验过程 1.先运行TCP代码,一人服务 ...
- 单利 复利计算器程序1.0 2.0 3.0 [ 合 ] 之 C语言
本程序用C语言编写~~~ 1.计算:本金为100万,利率或者投资回报率为3%,投资年限为30年,那么,30年后所获得的利息收入:按复利计算公式来计算就是:1,000,000×(1+3%)^30 1 v ...
- Gold Coins 分类: POJ 2015-06-10 15:04 16人阅读 评论(0) 收藏
Gold Coins Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 21767 Accepted: 13641 Desc ...
- HDU(2255),KM算法,最大权匹配
题目链接 奔小康赚大钱 Time Limit: 1000/1000MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
- 一种奇特的DEDE隐藏后门办法
转自:http://www.91ri.org/6462.html 一种奇特的DEDE隐藏后门办法 单位某站用的dedecms,今天被某黑阔getshell了,提交到了wooyun. 为了还原黑阔入 ...
- boost库学习之regex
一.背景 项目中许多地方需要对字符串进行匹配,比如根据指定的过滤字符串来过滤文件名.刚开始是排斥使用boost库的,第一,我不熟悉boost库:第二,如果引入第三方库,就会增加库的依赖,这样的后果是, ...