Codeforces Round #375 (Div. 2) - D
题目链接:http://codeforces.com/contest/723/problem/D
题意:给定n*m小大的字符矩阵。'*'表示陆地,'.'表示水域。然后湖的定义是:如果水域完全被陆地包围则称为湖。 海的定义:如果水域与任何一个边界的水域有连通则陈伟海。现在要求填一些湖使得最后湖的数量恰好等于K.输出最小要填多少个单位的'.'水域和最终填完之后的字符矩阵。
思路:水题。注意本题的连通是四个方向[上下左右],然后dfs每一个连通块,对于每个连通块我们维护2个值:水域的数目和连通块属于海还是湖。 假装最终一共有x个湖。则要填(x-k)个湖。所以对于湖我们找水域最小的哪些连通块来填。
#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<queue>
#include<math.h>
#include<time.h>
#include<map>
#include<vector>
#include<iostream>
using namespace std;
typedef long long int LL;
const int INF = 0x3f3f3f3f;
const int MAXN = + ;
int n, m, k, vis[MAXN][MAXN], id;
char G[MAXN][MAXN];
int dist[][] = { , , , -, , , -, };
struct Node{
int cnt; //水域数目
bool flag; //true: 海 false:湖
}sea[MAXN*MAXN];
bool check(int x, int y){ //是否越界
return x >= && x<n&&y >= && y<m;
}
void dfs(int x, int y){
int nextx, nexty;
for (int i = ; i<; i++){
nextx = x + dist[i][];
nexty = y + dist[i][];
if (check(nextx, nexty) && G[nextx][nexty] != '*'&&!vis[nextx][nexty]){
vis[nextx][nexty] = id; sea[id].cnt++;
if (nextx == || nextx == n - || nexty == || nexty == m - ){
sea[id].flag = true; //连通块包括边界。属于海
}
dfs(nextx, nexty);
}
}
}
int main(){
while (~scanf("%d%d%d", &n, &m, &k)){
for (int i = ; i<n; i++){
scanf("%s", G[i]);
}
memset(vis, , sizeof(vis)); id = ;
for (int i = ; i<n; i++){
if (i == || i == n - ){ continue; }
for (int j = ; j<m; j++){
if (j == || j == m - ){ continue; }
if (G[i][j] == '.'&&!vis[i][j]){
sea[id].cnt = ; sea[id].flag = false;
vis[i][j] = id; dfs(i, j); id++;
}
}
}
int totk = ;//统计有多少湖
for (int i = ; i<id; i++){
if (sea[i].flag == false){
totk++;
}
}
int ans = ; //统计要填多少单位的水域
for (; totk>k; totk--){
int minval = INF, minid = ;
for (int i = ; i<id; i++){//枚举每一个连通块,
if (sea[i].flag == false){//选择湖中水域最小的湖来填
if (sea[i].cnt<minval){
minval = sea[i].cnt; minid = i;
}
}
}
ans += minval; sea[minid].flag = true; //填完的湖标记一下。
for (int i = ; i<n; i++){
for (int j = ; j<m; j++){
if (vis[i][j] == minid){ //找到要填的湖的连通分量
G[i][j] = '*';
}
}
}
}
printf("%d\n", ans);
for (int i = ; i<n; i++){
printf("%s\n", G[i]);
}
}
return ;
}
Codeforces Round #375 (Div. 2) - D的更多相关文章
- 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 题意:给定一个字符串.只包含_,大小写字母,左右括号(保证不会出现括号里面套括号的情况),_分隔开单词 ...
- Codeforces Round #375 (Div. 2) - A
题目链接:http://codeforces.com/contest/723/problem/A 题意:在一维坐标下有3个人(坐标点).他们想选一个点使得他们3个到这个点的距离之和最小. 思路:水题. ...
- Codeforces Round #375 (Div. 2) F. st-Spanning Tree 生成树
F. st-Spanning Tree 题目连接: http://codeforces.com/contest/723/problem/F Description You are given an u ...
- Codeforces Round #375 (Div. 2) E. One-Way Reform 欧拉路径
E. One-Way Reform 题目连接: http://codeforces.com/contest/723/problem/E Description There are n cities a ...
- 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) B. Text Document Analysis 模拟
B. Text Document Analysis 题目连接: http://codeforces.com/contest/723/problem/B Description Modern text ...
- Codeforces Round #375 (Div. 2) A. The New Year: Meeting Friends 水题
A. The New Year: Meeting Friends 题目连接: http://codeforces.com/contest/723/problem/A Description There ...
- Codeforces Round #375 (Div. 2) Polycarp at the Radio 优先队列模拟题 + 贪心
http://codeforces.com/contest/723/problem/C 题目是给出一个序列 a[i]表示第i个歌曲是第a[i]个人演唱,现在选出前m个人,记b[j]表示第j个人演唱歌曲 ...
随机推荐
- rsync使用
1)拷贝本地文件.当SRC和DES路径信息都不包含有单个冒号":"分隔符时就启动这种工作模式. 如:rsync -a ./test.c /backup 2)使用一个远程 ...
- 在某公司时的java开发环境配置文档
1 开发环境配置 1.1. MyEclipse 配置 1.MyEclipse下载地址:\\server\共享文件\backup\MyEclipse9.0 2.修改工作空间编码为UTF-8,如下图 3 ...
- 浅析 Cordova for iOS
转自@夏小BO的技术博客: Cordova,对这个名字大家可能比较陌生,大家肯定听过 PhoneGap 这个名字,Cordova 就是 PhoneGap 被 Adobe 收购后所改的名字.(Cordo ...
- Xcode找不到模拟器
今天新建的工程,突然发现模拟器找不到了,之前遇到过忘记怎么解决了,于是再次记录下解决方法. 首先说下问什么找不到模拟器了,原因就是之前运行的版本和现在xcode的版本不同(的确,我从 Xcode7.3 ...
- [SQL Server]如何快速查找使用了某个字段的所有存储过程
[SQL Server]如何快速查找使用了某个字段的所有存储过程 当一个系统中使用了很多的表,并且存在大量的存储过程,当数据库中的某个表删除了某个字段,那么相应的存储过程也需要改动,但是我 ...
- xmpp SASL 定义
SASL 定义 <摘抄自:xmpp_3920> [SASL]的 profiling 需求要求协议定义 供以下信息: 服务名:“xmpp” 初始序列:初始实体 供一个开放 XML 流头后,并 ...
- jvm学习
一.jps主要用来输出JVM中运行的进程状态信息 jps [options] [hostid] 如果不指定hostid就默认为当前主机或服务器. Jps -ml 二.jstack主要用来查看某个Jav ...
- NYOJ题目10505C?5S?
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAscAAAJ/CAIAAAAbDelhAAAgAElEQVR4nO3dPXLbOhfG8XcT7r0Q11
- mysql 三个表连接查询
权限表(permission)10 字段名称 类型 约束 描述 authorityid integer Pk not null 权限流水号id PK userNameId int not nul ...
- 对Java内存模型即JMM的理解
类似物理上的计算机系统,Java虚拟机规范中也定义了一种Java内存模型,即Java Memory Model(JMM),来屏蔽掉各种硬件和操作系统的内存访问差异,以实现让Java程序在各种平台下都能 ...