题目链接: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的更多相关文章

  1. Codeforces Round #375 (Div. 2) - C

    题目链接:http://codeforces.com/contest/723/problem/C 题意:给定长度为n的一个序列.还有一个m.现在可以改变序列的一些数.使得序列里面数字[1,m]出现次数 ...

  2. Codeforces Round #375 (Div. 2) - B

    题目链接:http://codeforces.com/contest/723/problem/B 题意:给定一个字符串.只包含_,大小写字母,左右括号(保证不会出现括号里面套括号的情况),_分隔开单词 ...

  3. Codeforces Round #375 (Div. 2) - A

    题目链接:http://codeforces.com/contest/723/problem/A 题意:在一维坐标下有3个人(坐标点).他们想选一个点使得他们3个到这个点的距离之和最小. 思路:水题. ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. Codeforces Round #375 (Div. 2) B. Text Document Analysis 模拟

    B. Text Document Analysis 题目连接: http://codeforces.com/contest/723/problem/B Description Modern text ...

  8. 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 ...

  9. 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个人演唱歌曲 ...

随机推荐

  1. Xcode无法启动ios模拟器的问题

    一.问题描述 开发过程需要来回切换ios模拟器调试程序,开始在iPhone 4s下调试,然后切换到iPhone 6s Plus,再切换回iPhone 4s,遇到无法启动ios模拟器.错误提示如下: 二 ...

  2. HTML 样式表

    样式代码 样式代码与属性代码区别   样式代码必须带单位 属性不需要  多个样式代码之间用分号隔开  同一个样式里面 用空格  多个属性之间用空格隔开    举例 1.背景样式 .r { backgr ...

  3. 决绝Capturing 'demo' strongly in this block is likely to lead to a retain cycle

    - (IBAction)onTest:(id)sender { BlockDemo *demo = [[BlockDemo alloc]init];  __weak typeof(BlockDemo) ...

  4. Linux 底下使用C语言的 单链表 ,双链表,二叉树 读取文件,并排序

    直接上代码 单链表Linux读文件排序: 双链表Linux读取文件排序: 二叉树LinuX读取文件并排序:

  5. 20145206《Java程序设计》实验三实验报告

    20145206<Java程序设计>实验三实验报告 实验内容 XP基础 XP核心实践 相关工具 实验步骤 (一)敏捷开发与XP 软件工程是把系统的.有序的.可量化的方法应用到软件的开发.运 ...

  6. xdg-open filename 以相应的程序 打开文件

    [root@ok network-scripts]# xdg-open ifcfg-eth0

  7. Faster-rnnlm代码分析1 - 词表构建,Nnet成员

    https://github.com/yandex/faster-rnnlm     Gdb ./rnnlm r -rnnlm model-good.faster -train thread.titl ...

  8. Android init.rc执行顺序

    转自:http://blog.csdn.net/kickxxx/article/details/7590665 1. 所有的action运行于service之前 2.  下面为各个section的执行 ...

  9. Redis处理文件日志并发(2)

    多线程操作同一个文件时会出现并发问题.解决的一个办法就是给文件加锁(lock),但是这样的话,一个线程操作文件时,其它的都得等待,这样的话性能非常差.另外一个解决方案,就是先将数据放在队列中,然后开启 ...

  10. 网站对话框开源脚本--ArtDialog V6.0

    初识对话框脚本觉得artDialog还是挺不错的开源的js脚本,最新版本都是V6.0 ,相对之前版本,artDialog的语法也发生很大的变化,windows对应的JS版本如下 点击下载 语法也发生变 ...