解题思路:

  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. BZOJ 3052 树上带修莫队

    思路: 就是把带修莫队移到了树上 块的大小开到(n^2/3)/2 比较好- 这是一个卡OJ好题 //By SiriusRen #include <cmath> #include <c ...

  2. windows下安装ImageMagick扩展

    最近项目中需要用到图片的一些特殊处理——比如:根据用户请求生成任意尺寸的图像.经过一些资料的查找,最终选用了php_imagick.利用 ImageMagick,你可以根据web应用程序的需要动态生成 ...

  3. SQL Server 日常维护--查询当前正在执行的语句、死锁、堵塞

    查询当前正在执行的语句: SELECT der.[session_id],der.[blocking_session_id], sp.lastwaittype,sp.hostname,sp.progr ...

  4. Struts2简单环境搭建

    一.开篇 Struts2是一个运行于web容器的表示层框架,其核心作用是帮助我们处理Http请求.Struts2处理Http请求(Request),并进行内部处理,再进行Http返回. 下载strut ...

  5. 构建工具系列一--Travis-cli

    本文地址: http://www.cnblogs.com/blackmanba/articles/continuous-integration-tool-travis-cli.html或者http:/ ...

  6. SQL学习——基础语句(3)

    先上数据表 student表 grade 表 SQL Union 语句 合并两个或多个select查询结果集. select s_id from student union select  s_id ...

  7. thrift-go(golang)Server端笔记

      1.从thrift源码中拷贝go语言包(thrift\lib\go\thrift),放到go/src/下 2.新建go项目,实现server端服务接口   package main   impor ...

  8. swfupload组件上传文件

    前段时间做文件上传用的是H5的一个插件,由于浏览器的兼容性不好,所以又换了一个Flash版本的上传文件插件,感觉这个上传插件的使用方式跟H5的差不多,有些雷同.不过,由于后续浏览不再支持Flash(略 ...

  9. 【node.js web项目】解决路由默认是hash模式(带#)

    [概念讲述] 1.什么是hash模式 Vue+WebPack项目,本身是一个单页应用. vue-router 默认 hash 模式 —— 使用 URL 的 hash 来模拟一个完整的 URL,于是当 ...

  10. C# 正则表达式大全(转载)

    文章导读 正则表达式的本质是使用一系列特殊字符模式,来表示某一类字符串.正则表达式无疑是处理文本最有力的工具,而.NET提供的Regex类实现了验证正则表达式的方法.Regex 类表示不可变(只读)的 ...