题意:给定一个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搜索)的更多相关文章

  1. Codeforces 723D. Lakes in Berland

    解题思路: 1.dfs所有的水,顺便计数大小并判断是不是湖. 2.如果是湖,将大小和坐标存下来. 3.对湖按大小从小到大排序. 4.dfs前(湖的数量-k)个湖,用*填充这些湖. 代码: #inclu ...

  2. 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 ...

  3. CF723D. Lakes in Berland[DFS floodfill]

    D. Lakes in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  4. CodeForces - 930A Peculiar apple-tree(dfs搜索)

    题目: 给出一个树,这棵树上每个结点每一秒都会结出一颗果实,果实每经过一秒就会落向下一个结点,如果一个结点在同一时刻上的果实两两抵消,问最后在根节点处一共有多少个果实. 思路: dfs直接搜索统计这棵 ...

  5. D. Lakes in Berland (DFS或者BFS +连通块

    https://blog.csdn.net/guhaiteng/article/details/52730373 参考题解 http://codeforces.com/contest/723/prob ...

  6. 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 ...

  7. 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 ...

  8. 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 × ...

  9. 【29.70%】【codeforces 723D】Lakes in Berland

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

随机推荐

  1. linux下程序JDBC连接不到mysql数据库

    今天在linux下部署一个 JavaEE项目的时候总是连接不到Mysql数据库,检查之后发现连接池的配置确定是对的,进入linux服务器之后以mysql -uname -ppassword连接总是报A ...

  2. django学习之- 数据缓存

    5种配置:开发调试 CACHES = { 'default': { 'BACKEND': 'django.core.cache.backends.dummy.DummyCache', # 引擎内存CA ...

  3. Linux下异常信号

    我们介绍一些标准信号的名称以及它们代表的事件.每一个信号名称是一个代表正整数的宏,但是你不要试图去推测宏代表的具体数值,而是直接使用名称.这是因为这个数值会随不同的系统或同样系统的不同版本而不同,但是 ...

  4. 210 Course ScheduleII

    /* * 210 Course ScheduleII * 2016-6-9 by Mingyang * http://www.jyuan92.com/blog/leetcode-course-sche ...

  5. 关键字检索高亮标出-javasript/jQuery代码实现

    原文:http://www.open-open.com/code/view/1454504432089 此方法传入2个参数,一个是被检索内容所在的表单或者HTML元素的ID,另一为关键字,多个关键字的 ...

  6. linux安装mail服务使用外部MTA发送邮件

    阉割版的linux没有mail命令,也没有/etc/mail.rc文件 需要安装mail服务 yum install mailx.x86_64 几个概念:MUA.MTA.MDA 邮件用户代理(MUA, ...

  7. Android中传递对象的三种方法

    Android知识.前端.后端以至于产品和设计都有涉猎,想成为全栈工程师的朋友不要错过! Android中,Activity和Fragment之间传递对象,可以通过将对象序列化并存入Bundle或者I ...

  8. MySQL5.6 怎样优化慢查询的SQL语句 -- 慢日志介绍

    近期有个开发团队抱怨我们平台包括的mysql cluster不行,总是报mysql的"heartbeat Error".分析了他们收集的日志.没有发现mysql cluster节点 ...

  9. Navicat for MySQL出现#1045 错误怎么办

    #1045 - Access denied for user 'root'@'localhost' (using password: NO)这是因为你连接的时候没有密码或者密码没改对导致的.如下图所示 ...

  10. 《modern operating system》 chapter 6 DEADLOCKS 笔记

    DEADLOCKS Both processes are blocked and will remain so forever. This situation is called a deadlock ...