解题思路:

  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. Java 多线程(一)Thread

    线程的六种状态 1.新建状态 new了但是没有启动的线程的状态,如“Thread thread = new Thread()”,thread就是一个处于新建状态的线程. 2.运行状态 new出来的线程 ...

  2. <Android Framework 之路>Android5.1 Camera Framework(三)

    上一次讲解了一下startPreview过程,主要是为了画出一条大致的从上到下的线条,今天我们看一下Camera在Framework的sendCommand和dataCallback,这部分属于衔接过 ...

  3. python学习——正则表达式

    正则表达式                        正则表达式的主要作用就是对字符串进行匹配,通过匹配,进行筛选,选择出符合规则的字符串.其实正则表达式的作用就像是一个过滤器一样.你输入好自己的 ...

  4. 页面加载通过javascript来修改控件属性

    function changeFormElementStatus(tagNames) {            var tagNameArr = tagNames.split("," ...

  5. 路飞学城Python-Day38(第四模块思维导图)

  6. 路飞学城Python-Day22

  7. vscode代码格式化 空格的配置

    一个项目中同事使用webstorm,看我代码的时候说我传上去的会多出空格, VSCode 编辑 Setting.json文件,列出一些可以配置的项目   "javascript.format ...

  8. BZOJ 2754 [SCOI2012]喵星球上的点名 (AC自动机+map维护Trie树)

    题目大意:略 由于字符集大,要用map维护Trie树 并不能用AC自动机的Trie图优化,不然内存会炸 所以我用AC自动机暴跳fail水过的 显然根据喵星人建AC自动机是不行的,所以要根据问题建 然而 ...

  9. Swoole WebSoctet 使用 zlib 压缩之 PHP 与 pako.js

    一些理论知识 先说一下deflate算法吧,deflate是zip压缩文件的默认算法, 其实deflate现在不光用在zip文件中, 在7z, xz等其他的压缩文件中都用, 实际上deflate只是一 ...

  10. hive初体验

    --创建表 create table t_order(id int,name string,phone string) row format delimited fields terminated b ...