【Codeforces 723D】Lakes in Berland (dfs)
海洋包围的小岛,岛内的有湖,'.'代表水,'*'代表陆地,给出的n*m的地图里至少有k个湖,求填掉面积尽量少的水,使得湖的数量正好为k。
dfs找出所有水联通块,判断一下是否是湖(海水区非湖)。将湖按面积排序,若湖的数量为cnt,填掉前cnt-k个湖。
http://codeforces.com/problemset/problem/723/D
5 4 1*****..*******.*..**
1*****..*********..**
3 3 0****.****
1*********
#include<bits/stdc++.h> using namespace std; int n,m,k; char a[55][55]; bool vis[55][55]; int dx[6]={0,0,1,-1}; int dy[6]={1,-1,0,0};int num,cnt,islake; int ans; struct lake{int x,y;int num;int id; }lk[3600]; bool cmp(lake a,lake b){return a.num<b.num; } void dfs(int x,int y){vis[x][y]=1;num++;if(x==0||x==n-1||y==0||y==m-1)islake=0;for(int i=0;i<4;i++){int nx=x+dx[i],ny=y+dy[i];if(nx>=0&&nx<n&&ny<m&&ny>=0&&a[nx][ny]=='.'&&!vis[nx][ny])dfs(nx,ny);} } void fil(int x,int y,int id){vis[x][y]=1;ans++;a[x][y]='*';for(int i=0;i<4;i++){int nx=x+dx[i],ny=y+dy[i];if(nx>=0&&nx<n&&ny<m&&ny>=0&&a[nx][ny]=='.'&&!vis[nx][ny])fil(nx,ny,id);} } int main(){scanf("%d%d%d",&n,&m,&k);for(int i=0;i<n;i++)scanf(" %s",a[i]);for(int i=0;i<n;i++)for(int j=0;j<m;j++)if(!vis[i][j]&&a[i][j]=='.'){num=0;islake=1;dfs(i,j);if(islake)lk[cnt++]=(lake){i,j,num,cnt};}memset(vis,0,sizeof vis);sort(lk,lk+cnt,cmp);for(int l=0;l<cnt-k;l++)for(int i=0;i<n;i++)for(int j=0;j<m;j++)if(i==lk[l].x&&j==lk[l].y)fil(i,j,lk[l].id);printf("%d\n",ans);for(int i=0;i<n;i++)printf("%s\n",a[i]); }
转载于:https://www.cnblogs.com/flipped/p/5930002.html
【Codeforces 723D】Lakes in Berland (dfs)相关推荐
- 【29.70%】【codeforces 723D】Lakes in Berland
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【CodeForces - 1150C】Prefix Sum Primes(思维)
题干: We're giving away nice huge bags containing number tiles! A bag we want to present to you contai ...
- 【CodeForces - 244B】Undoubtedly Lucky Numbers (dfs打表 + 二分)
题干: Polycarpus loves lucky numbers. Everybody knows that lucky numbers are positive integers, whose ...
- 【CodeForces - 546C 】Soldier and Cards (模拟)
题干: Two bored soldiers are playing card war. Their card deck consists of exactly n cards, numbered f ...
- 【CodeForces - 312C】The Closest Pair (思维)
题干: Currently Tiny is learning Computational Geometry. When trying to solve a problem called "T ...
- 【CodeForces - 227C】Flying Saucer Segments (思维)
题干: An expedition group flew from planet ACM-1 to Earth in order to study the bipedal species (its r ...
- 【CodeForces - 1051C 】Vasya and Multisets (模拟)
题干: Vasya has a multiset ss consisting of nn integer numbers. Vasya calls some number xxnice if it a ...
- 【CodeForces - 764B 】Timofey and cubes (模拟)
题干: Young Timofey has a birthday today! He got kit of n cubes as a birthday present from his parents ...
- 【bzoj 2435】[Noi2011]道路修建(dfs)
2435: [Noi2011]道路修建 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 3505 Solved: 1167 [Submit][Sta ...
最新文章
- Maven 的这 7 个问题你思考过没有?
- STM32开发 -- Systick定时器
- 帝国cms熊掌号MIP主动推送教程
- Hello Dojo!(翻译)
- 【记录】Optisystem运行卡死,无法点击关闭、输入变量数值等问题解决方法
- 回顾jpg/png格式图片的区别
- 域名解析后主机记录和记录值怎么填写?
- ElasticSearch 可视化工具之cerebro
- BCEWithLogitsLoss= BCELoss + Sigmoid
- 初识Java ~ (二) # Java 中程序的执行流程,(万字长文)特别细~ 可收藏~
- mongoDB从入门到实战最全小白教程
- 百度Python工程师告诉你学好Python一定要养成的那些
- unity编辑器筛选或修改资源属性
- JVM-可达性分析算法
- 控制微电机综合实验装置,步进电动机,直流伺服电机,交流伺服电机QY-DQJ02
- windows phone真正解决方案:无法启动调试--未安装 Silverlight Developer 运行时。请安装一个匹配版本。
- 第四、第五套人民币“三字冠”的定名与组集
- pdf格式转换工具(PDF24 Creator 中文版) v6.3.2
- Python网络爬虫3 - 生产者消费者模型爬取某金融网站数据
- Java微信公众号开发之开源框架推荐