Codeforces 723D. Lakes in Berland
解题思路:
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的更多相关文章
- CodeForces 723D Lakes in Berland (dfs搜索)
题意:给定一个n*m的矩阵,*表示陆地, . 表示水,一些连通的水且不在边界表示湖,让你填最少的陆地使得图中湖剩下恰好为k. 析:很简单的一个搜索题,搜两次,第一次把每个湖的位置和连通块的数量记下来, ...
- 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 × ...
- 【29.70%】【codeforces 723D】Lakes in Berland
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- CF723D. Lakes in Berland[DFS floodfill]
D. Lakes in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
随机推荐
- springmvc生成注册验证码
通过SPRing MVC为系统添加验证码 1:布局登陆页面,用户名,密码,填写验证码的文本框,及验证码的图片及点击换图 <%@ taglib prefix="c" uri=& ...
- RecyclerView的刷新和加载更多
1.RecyclerView :出现也不知道多久了,没怎么使用过,上次写的笔记乱七八糟的,再次仔细的整理下. 使用需加入依赖 compile 'com.android.support:recyc ...
- 「JavaSE 重新出发」05.02 泛型数组列表、包装类
泛型数组列表 ArrayList 是一个采用类型参数(type parameter)的泛型类(generic class). java ArrayList<Employee> staff ...
- CDR 2017版本LiveSketch工具是什么,怎么用?
LiveSketch 工具在提供手绘草图的简便性和速度的同时,结合了智能笔触调整和向量绘图.在您绘制草图时,CorelDRAW 2017会分析您输入笔触的属性.时序和空间接近度,对其进行调整并将其转换 ...
- failed to push some refs to 'git@github.com:RocsSun/mytest.git
Git推送到GitHub仓库失败 使用Git将文件推送至GitHub的远程仓库时,报错failed to push some refs to 'git@github.com:RocsSun/mytes ...
- post数据html数据获取危险处理办法
基础小知识 ValidateRequest属性是Page类中比较常用的属性,用来指示是否对输入数据进行潜在危险性检查.在默认情况下为True,就是表示 “是对输入的数据进行潜在危险性检查”,这个属 ...
- centos7下安装pyspark
1.安装python 2.安装jdk 3.下载spark:http://spark.apache.org/downloads.html, 下载新版(spark-2.3.1-bin-hadoop2.7. ...
- [洛谷P3391]【模板】文艺平衡树(Splay)
题目大意:给定一个$1\sim n$的序列,每次翻转一个区间,输出最后的序列. 解题思路:Splay的区间翻转操作.我借此打了个Splay的模板(运用内存池,但有些功能不确定正确,例如单点插入). 大 ...
- PHP下的异步尝试四:PHP版的Promise
PHP下的异步尝试系列 如果你还不太了解PHP下的生成器和协程,你可以根据下面目录翻阅 PHP下的异步尝试一:初识生成器 PHP下的异步尝试二:初识协程 PHP下的异步尝试三:协程的PHP版thunk ...
- Vue生命周期函数的应用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...