链接:戳这里
D. Lakes in Berland
time limit per test:2 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output
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.
Input
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.
Output
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.
Examples
Input
5 4 1
****
*..*
****
**.*
..**
5 4 1
****
*..*
****
**.*
..**
Output
1
****
*..*
****
****
..**
1
****
*..*
****
****
..**
Input
3 3 0
***
*.*
***
3 3 0
***
*.*
***
Output
1
***
***
***
1
***
***
***
Note
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.
题意:
给出n*m的图,'*'表示陆地,'.'表示湖,整个图的周围是海洋,现在要求留下k个较大湖,连接海洋的湖不算湖
思路:
第一次bfs将湖分成块
第二遍bfs将边缘的湖变成海
然后把湖的大小拿出来排序,将前k-1小的变成陆地
代码:
#include <iostream>
#include <cstdio>
#include <queue>
#include <map>
#include <algorithm>
using namespace std;
int n,m,k;
char s[110][110];
int vis[110][110];
struct node{
int x,y;
node(int x=0,int y=0):x(x),y(y){}
};
int xx[4]={0,1,-1,0};
int yy[4]={1,0,0,-1};
bool pd(node t){
if(s[t.x][t.y]=='*' || vis[t.x][t.y]) return false;
if(t.x<1 || t.x>n || t.y<1 || t.y>m) return false;
return true;
}
bool pd1(node t,int num){
if(t.x<1 || t.x>n || t.y<1 || t.y>m) return false;
if(vis[t.x][t.y]==num) return true;
return false;
}
void bfs(node p,int num){
vis[p.x][p.y]=num;
queue<node> qu;
qu.push(p);
while(!qu.empty()){
node now=qu.front(),next;
qu.pop();
for(int i=0;i<4;i++){
next.x=now.x+xx[i];
next.y=now.y+yy[i];
if(pd(next)){
vis[next.x][next.y]=num;
qu.push(next);
}
}
}
}
void rebfs(node p,int num){
vis[p.x][p.y]=1;
queue<node> qu;
qu.push(p);
while(!qu.empty()){
node now=qu.front(),next;
qu.pop();
for(int i=0;i<4;i++){
next.x=now.x+xx[i];
next.y=now.y+yy[i];
if(pd1(next,num)){
vis[next.x][next.y]=1;
qu.push(next);
}
}
}
}
map<int , int > mp;
struct p{
int v,num;
p(int v=0,int num=0):v(v),num(num){}
bool operator < (const p &a)const{
return num<a.num;
}
}anw[10010];
int ans=0;
int main(){
scanf("%d%d%d",&n,&m,&k);
for(int i=1;i<=n;i++) {
getchar();
for(int j=1;j<=m;j++){
scanf("%c",&s[i][j]);
}
}
int cnt=1;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(vis[i][j]) continue;
else if(s[i][j]=='*'){
vis[i][j]=1;
} else {
bfs(node(i,j),++cnt);
}
}
}
for(int i=1;i<=m;i++){
if(vis[1][i]!=1){
rebfs(node(1,i),vis[1][i]);
}
if(vis[n][i]!=1){
rebfs(node(n,i),vis[n][i]);
}
}
for(int i=1;i<=n;i++){
if(vis[i][1]!=1){
rebfs(node(i,1),vis[i][1]);
}
if(vis[i][m]!=1){
rebfs(node(i,m),vis[i][m]);
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(vis[i][j]==1) continue;
mp[vis[i][j]]++;
}
}
for(auto & t : mp){
anw[++ans].v=t.first;
anw[ans].num=t.second;
}
sort(anw+1,anw+ans+1);
int ANS=0;
for(int l=1;l<=ans-k;l++){
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(vis[i][j]==anw[l].v){
s[i][j]='*';
ANS++;
}
}
}
}
printf("%d\n",ANS);
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
printf("%c",s[i][j]);
}
printf("\n");
}
return 0;
}