题目链接: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. javaURL中文乱码的问题

     中文乱码在java中URLEncoder.encode方法要调用两次解决   一.场景: 1.我在客户端要通过get方式调用服务器端的url,将中文参数做utf-8编码,需要在js中两次的进行编码, ...

  2. ajax加载模块实时刷新的原理

    var loadMenu = function(data) {        var trs = template.render('menu-list-temp', {'list': data});  ...

  3. 统计 F-test 和 T-test

    1 显著性差异 如果样本足够大,很容易有显著性差异.样本小,要有显著性差异很难. y是因变量,x是自变量 2 F-test与T-test Ftest也称ANOVA,是用来检测一个y下的不同level的 ...

  4. mysql 查看存储引擎的状态 show engine innodb status 详解

    首先,让我们来了解一下 SHOW INNODB STATUS 输出的基础,它打印了很多关于 InnoDB 内部性能相关的计数器.统计.事务处理信息等.在 MySQL 5 中,InnoDB 的性能统计结 ...

  5. SOCKet 编程 简介

    “一切皆Socket!” 话虽些许夸张,但是事实也是,现在的网络编程几乎都是用的socket. ——有感于实际编程和开源项目研究. 我们深谙信息交流的价值,那网络中进程之间如何通信,如我们每天打开浏览 ...

  6. hdu1492(约数个数定理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1492 这里先讲一下约数个数定理: 对于正整数x,将其质因分解为 x = pow(p1, a) * po ...

  7. UML类图五种关系与代码的对应关系

    转: UML类图中的五种关系的耦合强弱比较:依赖<关联<聚合<组合<继承 一.依赖关系: (一)说明 虚线+箭头 可描述为:Uses a 依赖是类的五种关系中耦合最小的一种关系 ...

  8. DOM - 5.事件冒泡 + 6.事件中的this

    5.事件冒泡 如果元素A嵌套在元素B中,那么A被点击不仅A的onclick事件会被触发,B的onclick也会被触发.触发的顺序是"由内而外" .验证:在页面上添加一个table. ...

  9. HTML5学习之视频与音频(三)

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  10. 排序练习【sdut 1582】【堆排序】

    排序 Time Limit: 1000ms   Memory limit: 32678K  有疑问?点这里^_^ 题目描述 给你N(N<=100)个数,请你按照从小到大的顺序输出. 输入 输入数 ...