解题思路:

  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. python黏包现象

    #黏包:发送端发送数据,接收端不知道应如何去接收造成的一种数据混乱现象. #关于分包和黏包: #黏包:发送端发送两个字符串"hello"和"word",接收方却 ...

  2. SQL 导出数据字典

    用于参考: SELECT       表名=case   when   a.colorder=1   then   d.name   else   ''   end,     表说明=case   w ...

  3. .NET中的异步操作及线程同步

    执行异步操作 CLR使用了WIN的线程处理能力,但保留了与其分离的权利.某些时候CLR的线程与Win的线程不是完全的匹配. 线程的系统开销较大,应限制其数量. 创建:分配并初始化一线程内核对象,保留1 ...

  4. appium连接夜游神的方法

    很多小伙伴想连接夜游神模拟器,但是无法连接,下面是夜游神的链接方法 第一步:先打开夜游神模拟器 第二步:打开运行输入cmd,输入夜游神连接方法:adb connect 127.0.0.1:62001第 ...

  5. Python 之 入门须知

    1.Python2.0不支持中文,3.0支持 2.版本问题

  6. innobackupex: Error: --decompress requires qpress

    数据库版本:5.6.16​系统版本:cenos 6.5​​通过percona-xtranbackup恢复数据库报错(软件版本:percona-xtrabackup-2.1.9-744.rhel6.x8 ...

  7. 聊聊 TCP 中的 KeepAlive 机制

    KeepAlive并不是TCP协议规范的一部分,但在几乎所有的TCP/IP协议栈(不管是Linux还是Windows)中,都实现了KeepAlive功能 RFC1122#TCP Keep-Alives ...

  8. Unity 指定区域随机实例化预制体Prefab 代码

    using UnityEngine; public class NewBehaviourScript : MonoBehaviour { public GameObject prefab; void ...

  9. Spring、Spring MVC、MyBatis 整合文件配置详解

    使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用法最好还是看官方文档. Spring:http://spring.io/docs MyBatis ...

  10. Java自定义属性注解

    代码: import static java.lang.annotation.RetentionPolicy.RUNTIME; import java.lang.annotation.ElementT ...