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个人演唱歌曲 ...
随机推荐
- 【leetcode】Sort List (middle)
Sort a linked list in O(n log n) time using constant space complexity. 思路: 用归并排序.设输入链表为S,则先将其拆分为前半部分 ...
- Sightseeing(poj 3463)
题意:给出n个点m条单向边,求最短路的道路条数和比最短路大1的道路条数的和. /* 用Dijkstra更新2*n次,来更新出所有点的最短路和次短路,顺便更新方案数. */ #include<cs ...
- 把Git Repository建到U盘上去(转)
把Git Repository建到U盘上去 转 把Git Repository建到U盘上去 Git很火.原因有三: 它是大神Linus Torvalds的作品,天然地具备神二代的气质和品质: 促进了生 ...
- 聊聊Android的APK反编译
上一篇<How To Use Proguard in Android APP>介绍了如何对Android进行混淆,现在来对它进行反编译看看,里面有些什么东西. APK文件,其实也是一个压缩 ...
- Apache commons-dbutils笔记
- 20145206邹京儒《Java程序设计》实验报告一:Java开发环境的熟悉(Windows+IDEA)
20145206<Java程序设计>实验报告一:Java开发环境的熟悉(Windows+IDEA) 实验内容及步骤 1.使用JDK编译.运行简单的Java程序: 建立实验目录: 在IDEA ...
- Jmeter 中使用非GUI启动进行压力测试
使用非 GUI 模式,即命令行模式运行 JMeter 测试脚本能够大大缩减所需要的系统资源.使用命令jmeter -n -t <testplan filename> -l <list ...
- 【ubuntu 】常见错误--Could not get lock /var/lib/dpkg/lock
ubuntu 常见错误--Could not get lock /var/lib/dpkg/lock 通过终端安装程序sudo apt-get install xxx时出错: E: Could not ...
- 判断一个类到底是从哪个jar包中调用的工具类
项目中使用的jar包较多时,会出现jar冲突的情况,有时候很难判断当前使用的这个类是从哪个jar包中调用的.因为一般我们只能看到jar包的名称,不清楚其中的类的目录结构. 这个类的作用就是说明当前调用 ...
- C#DataGridView合计处理
网上查了一些关于合计的代码 ,但发现大都都不尽人意,就自己再根据资料改了一下. #region 合计 //调用方法示例 //HeJi heji = null; //heji = new HeJi(la ...