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个人演唱歌曲 ...
随机推荐
- C#二维数组
数组格式 一维数组: Console.WriteLine("输入班级人数"); int renshu = int.Parse(Console.ReadLine()); ; i &l ...
- 【python】正则中的group()
来源:http://www.cnblogs.com/kaituorensheng/archive/2012/08/20/2648209.html 正则表达式中,group()用来提出分组截获的字符串, ...
- Please see the 'svn upgrade' command
svn: E155036: Please see the 'svn upgrade' command svn: E155036: Working copy '/home/easwy/dev' is t ...
- 什么是ORACLEASM
最直观的用途:共享一块磁盘,各个服务器做oracleasm即可共享 一. ASM(自动存储管理)的来由: ASM是Oracle 10g R2中为了简化Oracle数据库的管理而推出来的一项新功 ...
- java”伪“批量上传
jsp页面代码 <form method="post" action="" enctype="multipart/form-data" ...
- android viewPager 切换页面时防止fragment重新加载
把限制页面数设置成应用一共的页面数就可以了 ViewPager.setOffscreenPageLimit(3);
- Android WebView 支持H5的定位Js
//启用数据库 webSettings.setDatabaseEnabled(true); String dir = this.getApplicationContext().getDir(" ...
- monkey测试
一.理解monkey测试 1.Monkey测试是Android自动化测试的一种手段.Monkey测试本身非常简单,就是模拟用户的按键输入,触摸屏输入,手势输入等,看设备多长时间会出异常. 2.当Mon ...
- windows服务 - C# U盘升级
1.左侧工具栏里有Timer控件,但是如果调用的是系统时间,就需要添加System.timer.timer空间下的控件. 2.服务编写之后,还不能由SCM(服务控制管理器)进行管理,需要给该服务添 ...
- MVC公开课 – 1.基础 (2013-3-15广州传智MVC公开课)
1.MVC设计模式 Model 是指 要处理的业务代码和数据操作代码 View 视图 主要是指的 跟用户打交道 并能够展示数据 Controller 看成是 Model和View的桥梁 优点: 1 ...