首页 > 代码库 > cf723d Lakes in Berland
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;}
cf723d Lakes in Berland