Codeforces Round #375 (Div. 2) D. Lakes in Berland
来源:互联网 发布:淘宝宝贝展示框架代码 编辑:程序博客网 时间:2024/05/22 14:09
The map of Berland is a rectangle of the size n × m, which consists of cells of size1 × 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 beexactly k lakes in Berland. Note that the initial number of lakes on the map isnot 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
水题 :两边DFS,并记录足矣。.
代码:
#include <bits/stdc++.h>using namespace std;const int maxn=55;char s[maxn][maxn];int vis[maxn][maxn],n,m,k,notislake;typedef struct node{ int v,idx,idy; node (int x,int y,int vv) { idx=x; idy=y; v=vv; } bool operator < (const node &other) const { return v<other.v; }}Node;vector<Node>ve;int dfs(int x,int y){ if(vis[x][y]||s[x][y]!='.') return 0; vis[x][y]=1; if(x==1||x==n||y==1||y==m) notislake=1; return dfs(x+1,y)+dfs(x-1,y)+dfs(x,y+1)+dfs(x,y-1)+1;}int solve(int x,int y){ if(s[x][y]!='.') return 0; s[x][y]='*'; solve(x+1,y); solve(x-1,y); solve(x,y+1); solve(x,y-1);}int main(){ scanf("%d %d %d",&n,&m,&k); for(int i=1;i<=n;i++) { scanf("%s",s[i]+1); } notislake=0; for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { // cout<<s[i][j]; if(!vis[i][j]) { notislake=0; int ans=dfs(i,j); if(!notislake&&ans) ve.push_back(Node(i,j,ans)); } } } //cout<<ve.size()<<endl; sort(ve.begin(),ve.end()); int ans=0; for(int i=0;i<ve.size()-k;i++) ans+=ve[i].v; cout<<ans<<endl; for(int i=0;i<ve.size()-k;i++) { //cout<<ve[i].v<<endl; solve(ve[i].idx,ve[i].idy); } for(int i=1;i<=n;i++) { cout<<s[i]+1<<endl; } return 0;}
- 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 贪心
- 【Codeforces Round 375 (Div 2) D】【简单dfs】Lakes in Berland
- Codeforces Round #375 (Div. 2) D. Lakes in Berland __ dfs+贪心+小根堆
- Codeforces Round #375 (Div. 2) D - Lakes in Berland (dfs)
- Codeforces #375(Div.2)D. Lakes in Berland【Bfs】
- codeforces#375(div.2)723D - Lakes in Berland dfs+bfs
- Lakes in Berland(CF #375 Div. 2)
- 【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
- D. Lakes in Berland
- Ubuntu启动终端
- Spring整合Ehcache管理缓存
- iOS 产品上线BUG调试
- boa 流程分析
- find和xargs、exec、ok的故事
- Codeforces Round #375 (Div. 2) D. Lakes in Berland
- 跨域问题的解决方法
- 2016学习历程
- postgresql入门
- xcode——unable to boot the simulator解决方法
- (快速入门8)开发云识别应用
- 如何将光标移动到括号的外边?
- 多击事件的实现
- VirtualBox的Ubuntu显示界面过小解决措施