【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 写 ...
随机推荐
- java 22 - 13 多线程之解决线程安全问题的实现方式2
上一章说了,解决线程安全问题的实现方式1是使用同步代码块 同时也知道了,同步代码块的锁对象是任意对象:(Object obj ; Demo d;)这些都行 那么,现在来说解决线程安全问题的实现方式2 ...
- HTML 学习笔记 JavaScript (函数)
函数是由事件驱动的或者当他被调用时执行的可重复使用的代码块 实例 <!DOCTYPE html> <html> <head> <script> func ...
- Activity 与 Service 之间的消息传递
BgService代码 public class BgService extends Service { public static final String TAG = "BgServic ...
- Centos5.8 安装 Redmine
安装Ruby 到 /opt/ruby-2.0.0 -p481.tar.gz cd ruby--p481 ./configure --prefix=/opt/ruby- sudo make sudo m ...
- (原创)mybaits学习三,springMVC和mybatis融合
上一节,总计了spring和mybaits的融合,这一节,我们来学习springmvc和mybatis融合 最近在弄一个SSM的项目,然后在网上找资料,将资料总结如下 一,开发环境的配置 MyEcli ...
- BZOJ 2440 【中山市选2011】 完全平方数
Description 小 X 自幼就很喜欢数.但奇怪的是,他十分讨厌完全平方数.他觉得这些数看起来很令人难受.由此,他也讨厌所有是完全平方数的正整数倍的数.然而这丝毫不影响他对其他数的热爱. 这天是 ...
- shell 脚本替换文件中某个字符串
1.将当前目录下包含jack串的文件中,jack字符串替换为tom sed -i "s/jack/tom/g" `grep "jack" -rl ./` 2.将 ...
- [MetaHook] GameUI hook
Hook GameUI function. #include <metahook.h> #include <IGameUI.h> IGameUI *g_pGameUI = ; ...
- 轻量级iOS蓝牙库:LGBluetooth(项目中用过的)
LGBluetooth 是个简单的,基于块的,轻量级 CoreBluetooth 库: iOS 6引入了Core Bluetooth,让低功耗蓝牙设备之间的通信变得简单.但如果CoreBluetoot ...
- 使用EasyUI布局时出现混乱瞬间的解决方法
在所有form代码之前加遮罩层 <div id='PageLoadingTip' style="position: absolute; z-index: 1000; top: 0px; ...