海洋包围的小岛,岛内的有湖,'.'代表水,'*'代表陆地,给出的n*m的地图里至少有k个湖,求填掉面积尽量少的水,使得湖的数量正好为k。

dfs找出所有水联通块,判断一下是否是湖(海水区非湖)。将湖按面积排序,若湖的数量为cnt,填掉前cnt-k个湖。

http://codeforces.com/problemset/problem/723/D

Examples
input
5 4 1*****..*******.*..**

output
1*****..*********..**

input
3 3 0****.****

output
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)相关推荐

  1. 【29.70%】【codeforces 723D】Lakes in Berland

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  2. 【CodeForces - 1150C】Prefix Sum Primes(思维)

    题干: We're giving away nice huge bags containing number tiles! A bag we want to present to you contai ...

  3. 【CodeForces - 244B】Undoubtedly Lucky Numbers (dfs打表 + 二分)

    题干: Polycarpus loves lucky numbers. Everybody knows that lucky numbers are positive integers, whose ...

  4. 【CodeForces - 546C 】Soldier and Cards (模拟)

    题干: Two bored soldiers are playing card war. Their card deck consists of exactly n cards, numbered f ...

  5. 【CodeForces - 312C】The Closest Pair (思维)

    题干: Currently Tiny is learning Computational Geometry. When trying to solve a problem called "T ...

  6. 【CodeForces - 227C】Flying Saucer Segments (思维)

    题干: An expedition group flew from planet ACM-1 to Earth in order to study the bipedal species (its r ...

  7. 【CodeForces - 1051C 】Vasya and Multisets (模拟)

    题干: Vasya has a multiset ss consisting of nn integer numbers. Vasya calls some number xxnice if it a ...

  8. 【CodeForces - 764B 】Timofey and cubes (模拟)

    题干: Young Timofey has a birthday today! He got kit of n cubes as a birthday present from his parents ...

  9. 【bzoj 2435】[Noi2011]道路修建(dfs)

    2435: [Noi2011]道路修建 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 3505  Solved: 1167 [Submit][Sta ...

最新文章

  1. python判断质数的函数并输出_【python基础】|| 用python判断输入的数是否为素数
  2. maven项目下tomcat直接启动不了(LifecycleException)。报错如下截图
  3. [剑指offer]8.重建二叉树
  4. Ubuntu Server 分区案例
  5. CVPR2021 | MIMO-VRN 用于视频缩放任务的联合训练策略
  6. linux中c语言生日快乐_生日快乐,Linux:27岁
  7. 【工程项目经验】之C语言或汇编语言宏展开
  8. jenkins ssh 部署 Can't connect to server 错误
  9. 接口测试——Jmeter常见问题解决方法
  10. Node.js module.exports的用途是什么,如何使用它?
  11. c语言双精度型输出小数位数_4.1 C语言数据的输出
  12. 基本类型的默认值和取值范围
  13. JS 全局函数 isNaN()、parseInt()、parseFloat()
  14. PS、PR、AE、AI、AU区别
  15. Source Insight4.0的在整个工程中查找内容
  16. dispatch js实现_dva.js(and design)实现dispatch的回调函数
  17. flutter rpx屏幕适配方案
  18. php对接微信提醒,PHP对接微信公众平台消息接口开发流程教程
  19. 模型汇总-9 Variational AutoEncoder_VAE基础:LVM、MAP、EM、MCMC、Variational Inference(VI)
  20. 游戏c#脚本语言,C#作为脚本语言执行解密

热门文章

  1. MATLAB subplot、 stem
  2. 经典机器学习算法的数学推导
  3. java中几种读取配置文件的方法
  4. 安装Arcgis和ENVI后打开显示有关idl.dll的报错信息?
  5. Backup Exec 2010安装指南
  6. shader拖尾_Unity中拖尾渲染器使用解析
  7. Xv6操作系统导论(第三章)
  8. pycharm 中引用airtest中的air 文件
  9. 助力可观察性统一平台:SLS Trace服务发布
  10. java cursor属性_cursor的使用 怎么使用cursor 中的数据