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 cell is either land or water. The map is surrounded by the ocean.
Lakes are the maximal regions of water cells, connected by sides, which are not connected with the ocean. Formally, lake is a set of water cells, such that it's possible to get from any cell of the set to any other without leaving the set and moving only to cells adjacent by the side, none of them is located on the border of the rectangle, and it's impossible to add one more water cell to the set such that it will be connected with any other cell.
You task is to fill up with the earth the minimum number of water cells so that there will be exactly k lakes in Berland. Note that the initial number of lakes on the map is not less than k.
The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 50, 0 ≤ k ≤ 50) — the sizes of the map and the number of lakes which should be left on the map.
The next n lines contain m characters each — the description of the map. Each of the characters is either '.' (it means that the corresponding cell is water) or '*' (it means that the corresponding cell is land).
It is guaranteed that the map contain at least k lakes.
In the first line print the minimum number of cells which should be transformed from water to land.
In the next n lines print m symbols — the map after the changes. The format must strictly follow the format of the map in the input data (there is no need to print the size of the map). If there are several answers, print any of them.
It is guaranteed that the answer exists on the given data.
5 4 1*****..*******.*..**
1*****..*********..**
3 3 0****.****
1*********
In the first example there are only two lakes — the first consists of the cells (2, 2) and (2, 3), the second consists of the cell (4, 3). It is profitable to cover the second lake because it is smaller. Pay attention that the area of water in the lower left corner is not a lake because this area share a border with the ocean.
/* 不碰边界的连通块算是一个湖,给你要保留的湖的数目,一次填一个点,求填湖的最少次数 sb爆搜,注意最后那个贪心选湖,还有那个边界的判断,比赛写错了 */ #include<iostream> #include<cstdio> #include<string> #include<cstring> #include<cmath> #include<algorithm> using namespace std; struct orz{int p;int sz; }; orz lke[10005]; int mp[105][105],vis[105][105],cnt,n,m,k,tot; bool ok[10005],sgn[10005]; int dx[4] = {-1,0,1,0}; int dy[4] = {0,1,0,-1}; char cmd; bool jud(int y,int x){return y >= 1 && x >= 1 && y <= n && x <= m && !vis[y][x] && mp[y][x] == 1; } bool dfs(int y,int x){vis[y][x] = cnt;lke[cnt].sz++;int ny,nx;int gg = false;for(int i = 0;i < 4;i++){ny = y + dy[i];nx = x + dx[i];if(ny < 1 || ny > n || nx < 1 || nx > m) gg = true;if(jud(ny,nx)) if(!dfs(ny,nx)) gg = true;}if(gg) return false;else return true; } bool cmp(orz a,orz b){return a.sz < b.sz; } int main(){cin>>n>>m>>k;for(int i = 1;i <= n;i++){for(int j = 1;j <= m;j++){scanf("%c",&cmd);while(cmd != '*' && cmd != '.') scanf("%c",&cmd);if(cmd == '*') mp[i][j] = 2;else if(cmd == '.') mp[i][j] = 1;}}for(int i = 1;i <= n;i++){for(int j = 1;j <= m;j++){if(mp[i][j] == 1 && !vis[i][j]){cnt++;lke[cnt].p = cnt;ok[cnt] = dfs(i,j);if(ok[cnt]) tot++;}}}sort(lke+1,lke+1+cnt,cmp);int chs = 0,ans = 0;for(int i = 1;i <= cnt;i++){if(chs >= tot - k) break;if(ok[lke[i].p]){sgn[lke[i].p] = true;chs++;ans += lke[i].sz;}}cout<<ans<<endl;for(int i = 1;i <= n;i++){for(int j = 1;j <= m;j++){if(mp[i][j] == 2 || sgn[vis[i][j]]) cout<<"*";else cout<<".";}cout<<endl;}return 0; }
转载于:https://www.cnblogs.com/hyfer/p/5929951.html
cf723d Lakes in Berland相关推荐
- CF723D. Lakes in Berland[DFS floodfill]
D. Lakes in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 【29.70%】【codeforces 723D】Lakes in Berland
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- codeforces723 D. Lakes in Berland(并查集)
题目链接:codeforces723 D. Lakes in Berland 参考博客:http://www.cnblogs.com/Geek-xiyang/p/5930245.html 1 #inc ...
- Codeforeces - 723D -Lakes in Berland
Codeforeces - 723D -Lakes in Berland time limit per test2 seconds memory limit per test256 megabytes ...
- cf 723D Lakes in Berland(dfs)
D. Lakes in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 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 × ...
- Codeforces Round #375 (Div. 2) D. Lakes in Berland 并查集
http://codeforces.com/contest/723/problem/D 这题是只能把小河填了,题目那里有写,其实如果读懂题这题是挺简单的,预处理出每一块的大小,排好序,从小到大填就行了 ...
- Codeforces 723D. Lakes in Berland
解题思路: 1.dfs所有的水,顺便计数大小并判断是不是湖. 2.如果是湖,将大小和坐标存下来. 3.对湖按大小从小到大排序. 4.dfs前(湖的数量-k)个湖,用*填充这些湖. 代码: #inclu ...
- codeforces#375(div.2)723D - Lakes in Berland dfs+bfs
题意:给你一张n×m的图,点代表水,星代表地,当水连在一块时,称为湖,但是,若湖中有水在边界上,则不算它是湖,现在要求你去掉x个湖,并用地将其填上,使原图剩下k个湖,输出你最少需要多少个单位的地,并且 ...
最新文章
- 模拟实现: strstr strcpy strlen strcat strcmp memcpy memmove
- Matplotlib实例教程(十六)3D直方图
- .Net语言 APP开发平台——Smobiler学习日志:如何快速实现类似于微信的悬浮显示二维码效果...
- HTML导航页面结构
- java opencv人脸识别_java+opencv+intellij idea实现人脸识别
- 聊聊storm nimbus的LeaderElector
- 实验二+140+阮晨曦
- mysql主辅同步报错_mysql数据库主辅同步Slave_IO_Running,Slave_SQL_Running错误
- C/C++二维数组的传参方法总结
- JavaScript数字精度丢失的一些问题
- (转)ICO是区块链与生俱来的特性,是金融深化的终局
- 迅雷软件测试报告总结范文,软件测试-实验三
- python图像白色背景变透明
- js怎么获取扫码枪条码_JS 如何获取扫码枪输入数据
- java中数据库修改语句_java 修改数据库中的数据
- 超好用的Server酱,推送微信消息
- 天津大学仁爱学院ACM队简介
- 手机短信验证码开发流程
- 关于Python控制打印机如何横向打印EXEL
- 收藏!YOLOv4全文解读与翻译总结!(附思维导图和论文译文)