Codeforces 723D Lakes in Berland
来源:互联网 发布:lol免费刷皮肤软件 编辑:程序博客网 时间:2024/06/05 01:33
题目大意
给定n,m,k
n和m为岛屿的高和宽。k是要求最多剩下的湖的数量。
岛屿中,*代表陆地,.代表水。
湖的定义是一片连续的水(上下左右四个方向),并且水不含边界,水紧贴边界的情况被视为海。
问最少填多少湖的面积,使得湖的数量正好是k
每一个湖都有它自己的面积,而求的是湖的数量,所以说先求出填掉哪些湖
填湖的顺序无关紧要的话,一个贪心的想法是优先填掉那些面积小的湖
先一遍dfs求出每个湖的面积,用一个结构体保存每个湖的面积,以及这个湖任意一点的位置。
根据湖的面积从小到大排序,这样一直填到还剩k个湖的时候答案就出来了
#include <cstdio>#include <iostream>#include <algorithm>using namespace std;#define debug(x) std::cerr << #x << "=" << x << std::endl #define INF 2147483645const int MAXN = 60;int n,m,k,size,a[MAXN][MAXN],temp[MAXN][MAXN];int an,xx[MAXN],yy[MAXN],vis[MAXN][MAXN],ans;int dx[] = {0,1,0,-1};int dy[] = {1,0,-1,0}; bool flg;struct Num{ int nnn,xx,yy;}num[MAXN*MAXN];bool cmp(Num x, Num y) { return x.nnn < y.nnn; }void dfs(int x, int y) { if(x==n || y==m || x==1 || y==1) flg = false; vis[x][y] = 1; ans++; for(int i=0; i<4; i++) { int nx = x+dx[i], ny = y + dy[i]; if(nx>=1&&nx<=n&&ny>=1&&ny<=m && !vis[nx][ny] && a[nx][ny]) dfs(nx,ny); }}void dfs1(int x, int y) { a[x][y] = 0; for(int i=0; i<4; i++) { int nx = x+dx[i], ny = y + dy[i]; if(nx>=1&&nx<=n&&ny>=1&&ny<=m && a[nx][ny]) { dfs1(nx,ny); } }}int main() { cin >> n >> m >> k; for(int i=1; i<=n; i++) for(int j=1; j<=m; j++) { char c; cin >> c; if(c == '.') { a[i][j] = 1,temp[i][j] = 1; } } for(int i=1; i<=n; i++) for(int j=1; j<=m; j++) if(a[i][j] && !vis[i][j]) { ans = 0; flg = true; dfs(i,j); if(flg) { num[++size].nnn += ans; num[size].xx = i; num[size].yy = j; } } sort(num+1,num+1+size,cmp); for(int i=1; i<=size-k; i++) { an+=num[i].nnn; dfs1(num[i].xx, num[i].yy); } cout << an << endl; for(int i=1; i<=n; i++) { for(int j=1; j<=m; j++) if(a[i][j]) cout << '.'; else cout << '*'; cout << endl; }}
阅读全文
0 0
- 【29.70%】【codeforces 723D】Lakes in Berland
- codeforces 723D. Lakes in Berland [DFS]【】
- codeforces 723D Lakes in Berland
- Codeforces 723D - Lakes in Berland
- codeforces 723 D. Lakes in Berland
- CodeForces 723D Lakes in Berland DFS
- Codeforces 723D Lakes in Berland
- CodeForces 723D-Lakes in Berland(BFS DFS)
- Codeforces-723D- Lakes in Berland(DFS)
- D. Lakes in Berland
- D. Lakes in Berland
- Codeforeces - 723D -Lakes in Berland
- Codeforces #375(Div.2)D. Lakes in Berland【Bfs】
- [codeforces] D. Lakes in Berland [dfs and similar]
- Codeforces Round #375 (Div. 2) D. Lakes in Berland
- Codeforces Round #375 (Div. 2) D. Lakes in Berland
- Codeforces Round #375 (Div. 2) D. Lakes in Berland
- Codeforces Round #375 (Div. 2) D. Lakes in Berland 贪心
- 1098: 树状数组2(破坏公路)
- 第十三课 创建高级联结
- Linux环境下配置出现下列问题
- 读《大话处理器 》
- OpenCV学习之旅——滑动条的创建和使用
- Codeforces 723D Lakes in Berland
- man的汉化及使用
- 日记(周中)
- linux package
- 第一单元
- 一、Audio 介绍
- Java后台框架篇--Struts之简介
- I
- java io