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; + ; int n, m, k, vis[MAXN][MAXN], id; char G[MAXN][MAXN]; ][] = { , , , -, , , -, }; struct Node{ int cnt; //水域数目 bool flag; //true: 海 false:湖 }sea[MAXN*MAXN]; bool check(int x, int y){ //是否越界 && x<n&&y >= && y<m; } void dfs(int x, int y){ int nextx, nexty; ; 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++; || nextx == n - || nexty == || nexty == m - ){ sea[id].flag = true; //连通块包括边界。属于海 } dfs(nextx, nexty); } } } int main(){ while (~scanf("%d%d%d", &n, &m, &k)){ ; i<n; i++){ scanf("%s", G[i]); } memset(vis, , ; ; i<n; i++){ || i == n - ){ continue; } ; j<m; 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++; } } } ;//统计有多少湖 ; i<id; i++){ if (sea[i].flag == false){ totk++; } } ; //统计要填多少单位的水域 for (; totk>k; totk--){ ; ; 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; //填完的湖标记一下。 ; i<n; i++){ ; j<m; j++){ if (vis[i][j] == minid){ //找到要填的湖的连通分量 G[i][j] = '*'; } } } } printf("%d\n", ans); ; i<n; i++){ printf("%s\n", G[i]); } } ; }
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个人演唱歌曲 ...
随机推荐
- Hql 中 dao 层 以及daoimpl 层的代码,让mvc 模式更直观简洁
1.BaseDao接口: //使用BaseDao<T> 泛型 ,在service中注入的时候,只需要将T换为对应的bean即可 public interface BaseDao<T& ...
- UI:基础
App的生命周期 参考 多态的使用 // // main.m #import <Foundation/Foundation.h> #import "SingleDog.h&quo ...
- Velocity入门系列
Velocity介绍 Velocity是一个java模板引擎,通过简洁的语法可以返回动态内容给浏览器使用,本系类是基于velocity官方文档(就是照着翻译,同时对不清楚的地方进行详细讲解),其实技术 ...
- Opencv2系列学习笔记2(图像的其它操作)
本节主要涉及到图像的领域.算术操作以及如何操作图像感兴趣的区域. 一:邻域操作 以下例子主要对图像进行锐化.基于拉普拉斯算子<后面讨论>.这幅图像的边缘部分将得到放大,细节部分将更加的锐利 ...
- BZOJ 2209: [Jsoi2011]括号序列 [splay 括号]
2209: [Jsoi2011]括号序列 Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 1111 Solved: 541[Submit][Statu ...
- Windows中添加自己的程序到开机启动中(添加服务,添加注册表)
在系统启动的时候启动自己想要启动的程序: 方法一:利用开机启动文件夹 将exe文件或exe文件的快捷方式复制到(启动)文件夹下 以win7为例:开始→所有程序→启动→鼠标右键打开 方法二:添加系统服务 ...
- CentOS6.7 下安装JDK
第一步:从官网上下载rpm版本的jdk文件. 第二步:安装JDK 执行命令rpm -ivh jdk-8u73-linux-i586.rpm 至此,JDK安装完成,我们来看下JDK的安装目录 ...
- Java基础编程题——打印九九乘法表
package com.yangzl.basic; /** * 九九乘法表 * @author Administrator * */ public class Nine_Nine_Multiplica ...
- centos上shellcheck的安装
关于shellcheck的作用和功能,自行查阅. centos7 上安装shellcheck的过程中查了很多资料,大部分都是在ubunt下安装的,centos的比较少,然后好不容易看到一个https: ...
- C++航空系统
/* * SHA-256 implementation, Mark 2 * * Copyright (c) 2010,2014 Ilya O. Levin, http://www.literateco ...