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.
题目大意
填掉小的湖面 使数量精确等于k
水到极致了
但是比赛的时候把i写成0
上限写死 脑子犯浑。。
这个题明显dfs的话代码量会小一点,出于对并查集的熟悉还是先写了并查。。 代码炒鸡长而且丑 不过跑得飞快
啊啊啊啊啊,还是太菜了啊啊啊啊啊
#include <stdio.h>
#include <iostream>
#include <stack>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
#include<cctype>
using namespace std;
typedef long long ll;
int n,m,k;
char ch[][];
int val[][];
int par[];
int cnt[];
bool vis[];
void init()
{
memset(ch,'.',sizeof(ch));
//memset(vis,false,sizeof(vis));
int tot = ;
for(int i=;i<=n+;i++)
{
for(int j=;j<=m+;j++)
{
val[i][j] = ++tot;
}
}
for(int i=;i<=tot;i++)
{
par[i] = i;
cnt[i] = ;
}
}
int find(int x)
{
if(x==par[x]) return x;
return par[x] = find(par[x]);
}
void unite(int x,int y)
{
x = find(x);
y = find(y);
if(x==y) return ;
else{
par[x] = y;
cnt[y] += cnt[x];
}
}
void make(int i,int j)
{
int dx[] = {,,,-};
int dy[] = {,-,,};
for(int k=;k<;k++)
{
if(ch[dx[k]+i][dy[k]+j]=='.')
unite(val[i][j],val[dx[k]+i][dy[k]+j]);
}
}
vector<int>v;
bool cmp(int x,int y)
{
return cnt[x]<cnt[y];
}
int main()
{
scanf("%d%d%d",&n,&m,&k);
init();
for(int i=;i<=n+;i++)
{
unite(val[][],val[i][]);
unite(val[][],val[i][m+]);
}
for(int i=;i<=m+;i++)
{
unite(val[][],val[][i]);
unite(val[][],val[n+][i]);
}
for(int i=;i<=n;i++)
{
scanf("%s",ch[i]+);
ch[i][m+] = '.';
}
for(int i=;i<=n;++i)
for(int j=;j<=m;j++)
{
if(ch[i][j]=='.') make(i,j);
}
int tt = ;
for(int i=;i<=n;++i)
for(int j=;j<=m;j++)
{
int rt= find(val[i][j]);
if(ch[i][j]=='.'&&find(val[][])!=rt)
{
if(vis[rt]) continue;
else vis[rt] = true;
v.push_back(rt);
tt++;
}
}
sort(v.begin(),v.end(),cmp);
int len = v.size();
int ans = ;
int st = ;
while(tt>k)
{
for(int i=st;i<len;i++)
{
int rt = find(v[i]);
if(vis[rt])
{
tt--;
vis[rt] = false;
for(int i=;i<=n;++i)
for(int j=;j<=m;j++)
if(find(val[i][j]) == rt) {ch[i][j] = '*';ans++;}
break;
}
}
st++;
}
cout<<ans<<endl;
for(int i=;i<=n;++i)
{
for(int j=;j<=m;j++)
{
putchar(ch[i][j]);
}
putchar('\n');
} return ;
}
AC代码
Codeforces Round #375 (Div. 2) D. Lakes in Berland (DFS或并查集)的更多相关文章
- 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 贪心
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 并查集
http://codeforces.com/contest/723/problem/D 这题是只能把小河填了,题目那里有写,其实如果读懂题这题是挺简单的,预处理出每一块的大小,排好序,从小到大填就行了 ...
- Codeforces Round #286 (Div. 1) D. Mr. Kitayuta's Colorful Graph 并查集
D. Mr. Kitayuta's Colorful Graph Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/ ...
- 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]出现次数 ...
随机推荐
- mysql 数据库导入数据报错MySQL server has gone away解决办法
mysql 数据库导入数据报错MySQL server has gone away解决办法: 进入数据库执行以下命令即可: set global wait_timeout = 2880000; set ...
- BENZ Prognosis System C5 SD Be connected & Panasonic Xentry-Tab
Brand-new released!! Recently, we all autonumen website gives a brand-new Superstar prognosis bundle ...
- vue 的计算属性computed自我理解
类型:{ [key: string]: Function | { get: Function, set: Function } } 计算属性的结果会被缓存,除非依赖的响应式属性变化才会重新计算.注意, ...
- html自定义垂直导航菜单(加强版--自定义传入menu参数,支持JSONArray、JSArray、JSONObject、JSObject)
在上一篇中我简单写了个html自定义垂直导航菜单,缺点很明显,里面的数据是固定死的,不能动态更改数据. 这里我重写了一个修改版的垂直二级导航菜单,将原先的menuBox.init(config);修改 ...
- (转)Centos7.2 给grub菜单做加密
Centos7.2 给grub菜单做加密 原文:http://www.cnblogs.com/hanhy/articles/7274340.html#top 1.简述linux开机启动流程: 1) 启 ...
- Linux软件安装的补充
1 使用yum 命令查看软件提供的版本 yum list mysql* 然后比如说都需要安装我们就可以执行命令: yum install mysql* 然后就会安装所有的.会显示所有需要安装的包,和需 ...
- WPF 窗体在Alt+Tab中隐藏
问题: 近段时间由于项目上的需求,需要在WPF中使用COM组件,并且由于软件界面设计等等原因,需要将部分控件显示在COM组件之上,由于WindowsFormsHost的一些原因,导致继承在WPF中的W ...
- 常用shell脚本(持续更新中)
1. 写一个shell脚本来得到当前的日期,时间,用户名和当前工作目录. 答案 : 输出用户名,当前日期和时间,以及当前工作目录的命令就是logname,date,who i am和pwd. #!/b ...
- Dictionary集合 字典
1 Dictionary<int, string> dic = new Dictionary<int, string>(); dic.Add(,"张三"); ...
- Maven之依赖关系
在maven的管理体系中,各个项目组成了一个复杂的关系网,但是每个项目都是平等的,是个没有贵贱高低,众生平等的世界,全球每个项目从理论上来说都可以相互依赖.就是说,你跟开发Spring的大牛们平起平坐 ...