解题思路:

  1.dfs所有的水,顺便计数大小并判断是不是湖。

  2.如果是湖,将大小和坐标存下来。

  3.对湖按大小从小到大排序。

  4.dfs前(湖的数量-k)个湖,用*填充这些湖。

代码:

  

#include <bits/stdc++.h>
using namespace std;
typedef long long ll; struct lake{
int x;int y;
int size;
bool islake;
};
vector <lake> l; char a[][];
bool used[][];
int n,m,k;
int dir[][] = {,,-,,,,,-}; bool cmp(lake p,lake q){
return p.size < q.size;
} void dfs(int x,int y,lake &t){
// cout << x << " " << y << " l" << endl;
if(x == or y == or x == n or y == m) t.islake = false;
t.size++;
used[x][y] = true;
for(int i = ;i < ; ++i){
int conx = x+dir[i][];
int cony = y+dir[i][];
if(!used[conx][cony] and a[conx][cony] == '.'){
dfs(conx, cony, t);
}
}
} void dfsfill(int x,int y){
a[x][y] = '*';
for(int i = ;i < ; ++i){
int conx = x+dir[i][];
int cony = y+dir[i][];
if(a[conx][cony] == '.'){
dfsfill(conx, cony);
}
}
} int main(){
ios::sync_with_stdio(false);
cin >> n >> m >> k;
for(int i = ;i <= n; ++i) cin >> a[i]+;
for(int i = ;i <= n; ++i){
for(int j = ;j <= m; ++j){
if(!used[i][j] and a[i][j] == '.'){
lake t;
t.x = i;t.y = j;
t.size = ;
t.islake = true;
dfs(i, j, t);
if(t.islake){
// cout << t.x << " " << t.y <<" " << t.size << endl;
l.push_back(t);
}
}
}
}
sort(l.begin(),l.end(),cmp);
int s = l.size() - k;
int ans = ;
for(int i = ;i < s; ++i){
ans += l[i].size;
dfsfill(l[i].x, l[i].y);
}
cout << ans << endl;
for(int i = ;i <= n; ++i) cout << a[i]+ << endl;
return ;
}

Codeforces 723D. Lakes in Berland的更多相关文章

  1. CodeForces 723D Lakes in Berland (dfs搜索)

    题意:给定一个n*m的矩阵,*表示陆地, . 表示水,一些连通的水且不在边界表示湖,让你填最少的陆地使得图中湖剩下恰好为k. 析:很简单的一个搜索题,搜两次,第一次把每个湖的位置和连通块的数量记下来, ...

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

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

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

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

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

  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. cf723d Lakes in Berland

    The map of Berland is a rectangle of the size n × m, which consists of cells of size 1 × 1. Each cel ...

  9. CF723D. Lakes in Berland[DFS floodfill]

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

随机推荐

  1. MS SQL 获取数据字典的经典sql语句

    select [表名]=c.Name, [表说明]=isnull(f.[value],''), [列名]=a.Name, [列序号]=a.Column_id, [标识]=case when is_id ...

  2. 解决问题方法:没有设置对象,app.Config没有配置

  3. 基于 Web 的 Go 语言 IDE - Wide 1.3.0 发布!

    http://symphony.b3log.org/article/1437292757551 欢迎各位提意见.建议,参与到 Wide 开源项目中 :-)

  4. Date.getTime() 结果为 NaN

    yyyy-MM-dd 格式的时间,部分浏览器环境下转换为 Date 对象后调用 getTime() 方法的结果为 NaN. 需要将 - 替换为 / var dateStr = '2019-01-01' ...

  5. Day 02 - 01 计算机的操作系统

    操作系统 场景:有以下三个人 1.庄园主 -->相当于人类 2.管家 --> 把人类要传达的信息分配给奴隶,相当于操作系统 3.奴隶 -->为人类干活的机器 1.操作系统做了什么? ...

  6. Lua 中的 RSA 加解密实现

    记得之前,部门某款游戏陆陆续续收到一些玩家反馈,抱怨在登录游戏时会等待很久.初步排查后基本断定可能是此游戏的登录服务器程序某块代码有问题,于是即安排了服务器同事作排查分析但一直无果. 之后我时间有了空 ...

  7. Cookie和Session有什么区别

    1. 由于HTTP协议是无状态的协议,所以服务端需要记录用户的状态时,就需要用某种机制来识别具体的用户,这个机制就是Session.   典型的场景比如购物车,当你点击下单按钮时,由于HTTP协议无状 ...

  8. ExcelToHtmlTable转换算法:将Excel转换成Html表格并展示(项目源码+详细注释+项目截图)

    功能概述 Excel2HtmlTable的主要功能就是把Excel的内容以表格的方式,展现在页面中.Excel的多个Sheet对应页面的多个Tab选项卡.转换算法的难点在于,如何处理行列合并,将Exc ...

  9. FastDFS架构

    1.什么是 FastDFS FastDFS是用c语言编写的一款开源的分布式文件系统.FastDFS为互联网量身定制,充分考虑了冗余备份.负载均衡.线性扩容等机制,并注重高可用.高性能等指标,使用Fas ...

  10. nutch的一些基础整理

    nutch的一些基础整理 原创 2015年03月22日 18:18:01 标签: nutch / 240 编辑 删除 一.关于配置文件: nutch-default.xml:爬虫的默认配置.在${nu ...