题意:给定一个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. isinstance()和issubclass()

    内置函数中有个两个函数经常用到 isinstance()                    对象 是否是 类 的一个对象 from collections import Iterable prin ...

  2. Codeforces 658B Bear and Displayed Friends【set】

    题目链接: http://codeforces.com/contest/658/problem/B 题意: 给定元素编号及亲密度,每次插入一个元素,并按亲密度从大到小排序.给定若干操作,回答每次询问的 ...

  3. POJ 1753 Flip Game【枚举】

    题目链接: http://poj.org/problem?id=1753 题意: 由白块黑块组成的4*4方格,每次换一个块的颜色,其上下左右的块也会被换成相反的颜色.问最少换多少块,使得最终方格变为全 ...

  4. IOCP实现的任务队列

    unit IOCPQueue; interface uses windows, classes; type TOnQueueProc = procedure(sender: tobject; Para ...

  5. 关于Chrome谷歌浏览器开发者工具网络Network中返回无数据的问题

    1.如图所示,对于有些js文件,响应中无返回数据,Failed to load response data,当然本来是应该有数据,你用火狐浏览器看,就是有的,或者直接在浏览器地址栏里输入url,也可以 ...

  6. Tomcat+Servlet登录页面实例

    概念   Tomcat server是一个免费的开放源码的Web 应用server,属于轻量级应用server,在中小型系统和并发訪问用户不是非常多的场合下被普遍使用,是开发和调试JSP 程序的首选. ...

  7. [转]Wireshark抓包工具--TCP数据包seq ack等解读

    原文: http://blog.csdn.net/wang7dao/article/details/16805337/ ---------------------------------------- ...

  8. 选带傅里叶变换(zoom-fft)

    选带傅里叶变换的原理大家能够看书.大致的步骤为 移频 (将选带的中心频率移动到零频) 数字低通滤波器  (防止频率混叠) 又一次採样  (将採样的数据再次间隔採样,间隔的数据取决于分析的带宽,就是放大 ...

  9. Deepin-文件目录介绍

    请参见这篇文件:来自一个强大的网站 我主要介绍的就是: 下面所列文件,全部添加进了path目录(Linux查找命令,请参见man.linux,无论是find 或者是 which等) Deepin默认可 ...

  10. 【问题记录】LoadRunner 接口压测-json格式报文

    [问题起因] 前段时间,协助其他项目录制接口压测脚本,对方要求请求报文内容实现参数化. 请求方法如下: 直接在Parameter List中新增一个parameter, 将请求报文放入dat文件中.这 ...