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]出现次数 ...
随机推荐
- Redis主从、哨兵、集群的简单区别
2018-10-26 主从:读写分离,备份哨兵:监控,自动转移,选主集群:数据 hash 分片,同时包含主从及哨兵特性
- 解决 TwebBrowser 不能隐藏的问题
TOleControl(WebBrowser1).Visible := False
- 采用MQTT协议实现android消息推送(4)选fusesource-mqtt-client为客户端
1.简介 一个java写的mqtt客户端.项目地址: https://github.com/fusesource/mqtt-client 2.引入fusesource-mqtt-client库 Fil ...
- Django_Xadmin 修改后台
admin组件使用 Django 提供了基于 web页面的管理工具. Django 自动管理工具是 django.contrib 的一部分.你可以在项目的 settings.py 中的 INSTA ...
- GreenPlum 大数据平台--备份-邮件配置-gpcrondump & gpdbrestore(五)
01,备份 生成备份数据库 [gpadmin@greenplum01 ~]$ gpcrondump -l /gpbackup/back2/gpcorndump.log -x postgres -v [ ...
- DB Intro - MongoDB Relations
https://www.quackit.com/mongodb/tutorial/mongodb_create_a_relationship.cfm
- PHP 网页调用本地exe程序实例
一.需求:在做网站的时候,有些网站网页面需要调用本地的exe程序. 二.方法:利用注册URL Protocol的方式. 代码如下: 1.视图文件里面的代码: <a href="fyex ...
- PHP流程控制之goto语句
goto 操作符可以用来跳转到程序中的另一位置.该目标位置可以用目标名称加上冒号来标记,而跳转指令是 goto 之后接上目标位置的标记.PHP 中的 goto 有一定限制,目标位置只能位于同一个文件和 ...
- Guid和Oracle中16进制字符的转换
我们知道在Oracle中存的guid是16进制字符串,而在我们的C#代码中存的是guid对象,这样我会就要进行转换, 下面给出了两者进行转换的方法: public class Guid2RawProc ...
- 深入理解JavaScript系列(11):执行上下文(Execution Contexts)
简介 从本章开始,我将陆续(翻译.转载.整理)http://dmitrysoshnikov.com/网站关于ECMAScript标标准理解的好文. 本章我们要讲解的是ECMAScript标准里的执行上 ...