CodeForces 723D Lakes in Berland (dfs搜索)
题意:给定一个n*m的矩阵,*表示陆地, . 表示水,一些连通的水且不在边界表示湖,让你填最少的陆地使得图中湖剩下恰好为k。
析:很简单的一个搜索题,搜两次,第一次把每个湖的位置和连通块的数量记下来,第二次去填陆地,选少的进行填。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
//#include <tr1/unordered_map>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
//using namespace std :: tr1; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 2e3 + 5;
const LL mod = 10000000000007;
const int N = 1e6 + 5;
const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
inline LL gcd(LL a, LL b){ return b == 0 ? a : gcd(b, a%b); }
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
char s[55][55];
struct Node{
int x, y, cnt;
Node(int xx, int yy, int c) : x(xx), y(yy), cnt(c) { }
bool operator < (const Node &p) const{
return cnt < p.cnt;
}
};
bool vis[55][55];
int ans, cnt; void dfs(int r, int c){
ans = Max(ans, cnt);
for(int i = 0; i < 4; ++i){
int x = r + dr[i];
int y = c + dc[i];
if(x < 0 || x >= n || y < 0 || y >= m){ ans = INF; continue; }
if(vis[x][y] || s[x][y] == '*') continue;
vis[x][y] = true;
++cnt;
dfs(x, y);
}
} void dfs1(int r, int c){
for(int i = 0; i < 4; ++i){
int x = r + dr[i];
int y = c + dc[i];
if(!is_in(x, y) || s[x][y] == '*') continue;
s[x][y] = '*';
dfs1(x, y);
}
} int main(){
int k;
while(scanf("%d %d %d", &n, &m, &k) == 3){
for(int i = 0; i < n; ++i) scanf("%s", s+i); vector<Node> v;
memset(vis, false, sizeof vis);
for(int i = 0; i < n; ++i){
for(int j = 0; j < m; ++j){
if(s[i][j] == '.' && !vis[i][j]){
ans = 0;
cnt = 1;
vis[i][j] = true;
dfs(i, j);
if(ans != INF) v.push_back(Node(i, j, ans));
}
}
} sort(v.begin(), v.end());
ans = 0;
for(int i = 0; i+k < v.size(); ++i){
s[v[i].x][v[i].y] = '*';
dfs1(v[i].x, v[i].y);
ans += v[i].cnt;
} printf("%d\n", ans);
for(int i = 0; i < n; ++i)
puts(s[i]);
}
return 0;
}
CodeForces 723D Lakes in Berland (dfs搜索)的更多相关文章
- Codeforces 723D. Lakes in Berland
解题思路: 1.dfs所有的水,顺便计数大小并判断是不是湖. 2.如果是湖,将大小和坐标存下来. 3.对湖按大小从小到大排序. 4.dfs前(湖的数量-k)个湖,用*填充这些湖. 代码: #inclu ...
- Codeforces Round #375 (Div. 2) D. Lakes in Berland dfs
D. Lakes in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- CF723D. Lakes in Berland[DFS floodfill]
D. Lakes in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- CodeForces - 930A Peculiar apple-tree(dfs搜索)
题目: 给出一个树,这棵树上每个结点每一秒都会结出一颗果实,果实每经过一秒就会落向下一个结点,如果一个结点在同一时刻上的果实两两抵消,问最后在根节点处一共有多少个果实. 思路: dfs直接搜索统计这棵 ...
- D. Lakes in Berland (DFS或者BFS +连通块
https://blog.csdn.net/guhaiteng/article/details/52730373 参考题解 http://codeforces.com/contest/723/prob ...
- Codeforces Round #375 (Div. 2)——D. Lakes in Berland(DFS连通块)
D. Lakes in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Round #375 (Div. 2) D. Lakes in Berland (DFS或并查集)
D. Lakes in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- codeforces 723D: Lakes in Berland
Description The map of Berland is a rectangle of the size n × m, which consists of cells of size 1 × ...
- 【29.70%】【codeforces 723D】Lakes in Berland
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
随机推荐
- 1716: [Usaco2006 Dec]The Fewest Coins 找零钱
n<=100种硬币,给每种的硬币的面额<=120和我每种有多少个<=10000,店主的硬币跟我一样但有无限个,求买t<=10000块钱的东西钱最少转手几次. 我拿的硬币最少几次 ...
- input文本框的value属性在页面中不随输入的数据而变化
今天,在做试验遇到这么一个需求: 一个input文本框,输入值后将标签传到后台,在后台解析标签,发现value仍然是初值,不是我们改变后的值. 例如: <input name="&qu ...
- CPM、CPC、CPA、PFP、CPS、CPL、CPR介绍
一个网络媒体(网站)会包含有数十个甚至成千上万个页面,网络广告所投放的位置和价格 就牵涉到特定的页面以及浏览人数的多寡.这好比平面媒体(如报纸)的“版位”.“发行 量”,或者电波媒体(如电视)的“时段 ...
- 0c-适配 iOS 11
参考路径:https://mp.weixin.qq.com/s?__biz=MzA3NTYzODYzMg==&mid=2653579210&idx=1&sn=d5ea8d46c ...
- T2627 村村通 codevs
http://codevs.cn/problem/2627/ 时间限制: 1 s 空间限制: 32000 KB 题目等级 : 黄金 Gold 题目描述 Description 农民约翰被选为他们 ...
- 将Sublime Text 2搭建成一个好用的IDE(转)
原文地址 将Sublime Text 2搭建成一个好用的IDE 说起编辑器,可能大部分人要推荐的是Vim和Emacs,本人用过Vim,功能确实强大,但是不是很习惯,之前一直有朋友推荐SUblime T ...
- Hive安装中遇到过的坑
实现说明每一个用户的环境都有细微的不一致,所以这里只是个人经过这些坑的处理,但是不意味着所有处理都是这样的操作,仅作为参考. 第一个坑 数据库安装,数据库最好装在Linux上,一直出了很多错,这里有一 ...
- 【.Net Core 学习系列】-- EF Core实践(DB First)
一.开发环境: VS2015, .Net Core 1.0.0-preview2-003156 二.准备数据: CREATE DATABASE [Blogging]; GO USE [Blogging ...
- 新手玩个人server(阿里云)续二
小二班一番厮杀:那英四强诞生:大家闺秀,小家碧玉.窈窕淑女,妍姿俊俏 .不解释! ?不行! 陈冰,李嘉格,刘明湘.张碧晨.大多数的时候,仅仅要脸好看,一切都那么自热而然的顺理成章. 尽管网上骂声四起, ...
- Android 使用图片异步载入框架Universal Image Loader的问题
使用的Jar包 问题: optionsm = new DisplayImageOptions.Builder() .displayer(new RoundedBitmap ...