解题思路:

  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[55][55];
bool used[55][55];
int n,m,k;
int dir[4][2] = {1,0,-1,0,0,1,0,-1};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 == 1 or y == 1 or x == n or y == m) t.islake = false;t.size++;used[x][y] = true;for(int i = 0;i < 4; ++i){int conx = x+dir[i][0];int cony = y+dir[i][1];if(!used[conx][cony] and a[conx][cony] == '.'){dfs(conx, cony, t);}}
}void dfsfill(int x,int y){a[x][y] = '*';for(int i = 0;i < 4; ++i){int conx = x+dir[i][0];int cony = y+dir[i][1];if(a[conx][cony] == '.'){dfsfill(conx, cony);}}
}int main(){ios::sync_with_stdio(false);cin >> n >> m >> k;for(int i = 1;i <= n; ++i) cin >> a[i]+1;     for(int i = 1;i <= n; ++i){for(int j = 1;j <= m; ++j){if(!used[i][j] and a[i][j] == '.'){lake t;t.x = i;t.y = j;t.size = 0;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 = 0;for(int i = 0;i < s; ++i){ans += l[i].size;dfsfill(l[i].x, l[i].y);}cout << ans << endl;for(int i = 1;i <= n; ++i) cout << a[i]+1 << endl;return 0;
}

Codeforces 723D. Lakes in Berland相关推荐

  1. CodeForces - 723D Lakes in Berland dfs

    Description The map of Berland is a rectangle of the size n × m, which consists of cells of size 1 × ...

  2. Codeforeces - 723D -Lakes in Berland

    Codeforeces - 723D -Lakes in Berland time limit per test2 seconds memory limit per test256 megabytes ...

  3. cf 723D Lakes in Berland(dfs)

    D. Lakes in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  4. codeforces#375(div.2)723D - Lakes in Berland dfs+bfs

    题意:给你一张n×m的图,点代表水,星代表地,当水连在一块时,称为湖,但是,若湖中有水在边界上,则不算它是湖,现在要求你去掉x个湖,并用地将其填上,使原图剩下k个湖,输出你最少需要多少个单位的地,并且 ...

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

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

  6. 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 ...

  7. codeforces723 D. Lakes in Berland(并查集)

    题目链接:codeforces723 D. Lakes in Berland 参考博客:http://www.cnblogs.com/Geek-xiyang/p/5930245.html 1 #inc ...

  8. CF723D. Lakes in Berland[DFS floodfill]

    D. Lakes in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  9. 【Codeforces 723D】Lakes in Berland (dfs)

    海洋包围的小岛,岛内的有湖,'.'代表水,'*'代表陆地,给出的n*m的地图里至少有k个湖,求填掉面积尽量少的水,使得湖的数量正好为k. dfs找出所有水联通块,判断一下是否是湖(海水区非湖).将湖按 ...

最新文章

  1. Ubuntu搭建Spark运行环境
  2. 最短路的那些有趣的模型(不定期更新)
  3. 7-37 整数分解为若干项之和(20 分)
  4. 云计算与springCloud概念上的区别
  5. 51nod-1640--天气晴朗的魔法(简单最小生成树)
  6. Maven项目 之eclipse操作篇
  7. P3180-[HAOI2016]地图【圆方树,莫队,分块】
  8. javafx 剪切板_JavaFX技巧18:路径剪切
  9. springboot全局常量_Spring-Boot配置属性和环境变量的加载顺序
  10. android 崩溃,android 9出现崩溃
  11. Win32汇编——过程控制(进程调试和进程隐藏)
  12. c语言格式化浮点数多余的0
  13. php 英文转中文,php 中英文语言转换的方法
  14. linux嵌入式主要学什么软件,嵌入式软件工程师需要学什么?嵌入式Linux开发的工作内容...
  15. unity实现简单游戏——井字棋
  16. 【Java学习002】Java-ArrayList源码解析
  17. 3D打印技术新进展,正带来哪些产业新机会?
  18. stm32f767之PWM
  19. php如何设置随机数字和字母,PHP获取随机数字和字母的方法大全
  20. 条形码识别软件linux,条形码生成和识别库 Aspose.BarCode

热门文章

  1. ubuntu设置始终亮屏_如何设置默认显示亮度?
  2. whiel oracle,Oracle中的for和while循环
  3. 判断给定的二叉树是否为二叉排序树
  4. 作业一 郝树伟 1101210664
  5. 40.QT-QPropertyAnimationdong和QParallelAnimationGroup动画实现
  6. hihocoder #1388 : Periodic Signal NTTFFT
  7. Runtime.getRuntime().exec()
  8. Ubuntu SSH root user cannot login
  9. oracle数据库元数据SQL查询
  10. 安卓下的免费离线地图导航软件(老虎地图)测试及网盘下载