【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[][];
bool vis[][];
int dx[]={,,,-};
int dy[]={,-,,}; int num,cnt,islake;
int ans;
struct lake{
int x,y;
int num;
int id;
}lk[];
bool cmp(lake a,lake b){
return a.num<b.num;
}
void dfs(int x,int y){
vis[x][y]=;
num++;
if(x==||x==n-||y==||y==m-)islake=;
for(int i=;i<;i++){
int nx=x+dx[i],ny=y+dy[i];
if(nx>=&&nx<n&&ny<m&&ny>=&&a[nx][ny]=='.'&&!vis[nx][ny])
dfs(nx,ny);
}
}
void fil(int x,int y,int id){
vis[x][y]=;
ans++;
a[x][y]='*';
for(int i=;i<;i++){
int nx=x+dx[i],ny=y+dy[i];
if(nx>=&&nx<n&&ny<m&&ny>=&&a[nx][ny]=='.'&&!vis[nx][ny])
fil(nx,ny,id);
}
}
int main(){
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<n;i++)
scanf(" %s",a[i]);
for(int i=;i<n;i++)
for(int j=;j<m;j++)
if(!vis[i][j]&&a[i][j]=='.'){
num=;
islake=;
dfs(i,j);
if(islake)lk[cnt++]=(lake){i,j,num,cnt};
}
memset(vis,,sizeof vis);
sort(lk,lk+cnt,cmp);
for(int l=;l<cnt-k;l++)
for(int i=;i<n;i++)
for(int j=;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=;i<n;i++)
printf("%s\n",a[i]);
}
【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 697C】Lorenzo Von Matterhorn(LCA)
Least Common Ancestors 节点范围是1~1e18,至多1000次询问. 只要不断让深的节点退一层(>>1)就能到达LCA. 用点来存边权,用map储存节点和父亲连边的权 ...
- 【CodeForces 266C】Below the Diagonal(模拟)
题目每次把空列换到最后一列,把非空行换到最下一行. #include<cstdio> #include<algorithm> #define N 1005 using name ...
- 【POJ - 1426】Find The Multiple(dfs)
-->Find The Multiple 原文是英语,直接上中文了 Descriptions: 给定一个正整数n,请编写一个程序来寻找n的一个非零的倍数m,这个m应当在十进制表示时每一位上只包含 ...
- 【Unity Shaders】学习笔记——SurfaceShader(十一)光照模型
[Unity Shaders]学习笔记——SurfaceShader(十一)光照模型 转载请注明出处:http://www.cnblogs.com/-867259206/p/5664792.html ...
- 【Unity Shaders】学习笔记——SurfaceShader(十)镜面反射
[Unity Shaders]学习笔记——SurfaceShader(十)镜面反射 如果你想从零开始学习Unity Shader,那么你可以看看本系列的文章入门,你只需要稍微有点编程的概念就可以. 水 ...
- 【Unity Shaders】学习笔记——SurfaceShader(九)Cubemap
[Unity Shaders]学习笔记——SurfaceShader(九)Cubemap 如果你想从零开始学习Unity Shader,那么你可以看看本系列的文章入门,你只需要稍微有点编程的概念就可以 ...
- 【Unity Shaders】学习笔记——SurfaceShader(八)生成立方图
[Unity Shaders]学习笔记——SurfaceShader(八)生成立方图 转载请注明出处:http://www.cnblogs.com/-867259206/p/5630261.html ...
- 【Unity Shaders】学习笔记——SurfaceShader(七)法线贴图
[Unity Shaders]学习笔记——SurfaceShader(七)法线贴图 转载请注明出处:http://www.cnblogs.com/-867259206/p/5627565.html 写 ...
随机推荐
- url编码base编码解码十六进制
0x25346425353425343525333525343325366125343525373725346425353125366625373825346425343425363725346225 ...
- ASP.NET后台获取cookie中文乱码解决办法
项目中有一功能,需要从一个页面前台使用cookie保存json数据,并传递到第二个页面.要在第二个页面中获取cookie中的json的值,没有任何处理情况下,获取的字符串为乱码,就连符号都是乱码的.百 ...
- 修复垂直滑动RecyclerView嵌套水平滑动RecyclerView水平滑动不灵敏问题
在 Android 应用中,大部分情况下都会使用一个垂直滚动的 View 来显示内容(比如 ListView.RecyclerView 等).但是有时候你还希望垂直滚动的View 里面的内容可以水平滚 ...
- Cordova - 使用Cordova开发iOS应用实战5(获取手机里照片,并编辑)
使用Cordova可以很方便的通过js代码读取系统相簿里面的照片,同使用设备摄像头拍照一样,同样需要先添加camera插件. 一,添加camera插件 首先我们要在“终端”中进入工程所在的目录,然后运 ...
- 通用权限管理系统组件3.9 的 Oracle 数据库创建脚本参考
---------------------------------------------------- -- Export file for user USERCENTER -- -- Create ...
- cpu负载和利用率
理解Linux系统负荷 linux里的CPU负载
- android 获取当前系统时间
取得系统时间 1. long time=System.currentTimeMillis(); 2. final Calendar mCalendar=Calendar.getInstance(); ...
- TinyFrame升级之六:全局日志的设计及实现
日志记录显然是框架设计中不可或缺的元素,在本框架中,我们将使用log4net作为日志记录的主体.下面来具体说明如何让框架继承log4net,并通过Autofac进行IOC注入. 首先,定义好我们的Lo ...
- 【Android测试】【随笔】Android Studio环境搭建
◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/5482778.html 随着Android Studio的推 ...
- FineUI v4.0.2 (beta) 发布了!
FineUI v4.0.2 (beta) 已经于 2013-12-15 发布! ================================== 关于FineUI基于 ExtJS 的开源 ASP. ...