【Codeforces-723D】Lake in Berland

来源:互联网 发布:阿里云 协会 备案 编辑:程序博客网 时间:2024/05/29 04:02

点击打开链接

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.


Example
Input
5 4 1
****
*..*
****
**.*
..**
Output
1
****
*..*
****
****
..**
Input
3 3 0
***
*.*
***
Output
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,N行M列,K是要留下来的湖泊数。首先规定给出的湖泊数要>=k,能连在一起的叫做一个湖泊,并且在边上的湖泊叫海洋,不算湖泊,你的任务就是把多出来的湖泊用*填充,只留下K个湖泊。

AC代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n,m,k,ex,ey,ans,t,sum=0;
char s[55][55];
int vis[55][55];
int fx[4]={0,0,1,-1},fy[4]={1,-1,0,0};
struct possion
{
int x;
int y;
int ant;
}a[3025];
bool check(int x,int y)
{
if(x>=n||y>=m||x<0||y<0||vis[x][y]||s[x][y]=='*')
return false;
return true;
}
bool cmp(possion a,possion b)
{
return a.ant >b.ant ;
}
void dfs(int x,int y)
{
vis[x][y]=1;
for(int i=0;i<4;i++)
{
int xx=x+fx[i],yy=y+fy[i];
if(!check(xx,yy))
continue ;
if(s[xx][yy]=='.')

  ans++; 
  dfs(xx,yy);

}

}
void Dfs(int x,int y)
{
s[x][y]='*';
for(int i=0;i<4;i++)
{
int xx=x+fx[i],yy=y+fy[i];
if(xx>=0&&yy>=0&&yy<m&&xx<n&&s[xx][yy]=='.')
  Dfs(xx,yy);
}

}
int main()
{
scanf("%d%d%d",&n,&m,&k);
memset(vis,0,sizeof(vis));
for(int i=0;i<n;i++)
scanf("%s",s[i]);
t=0;
for(int i=0,j=0;j<m;j++)
{
if(!vis[i][j])
{
if(s[i][j]=='.')
{
dfs(i,j);
}
}
}
for(int j=0,i=0;i<n;i++)
{
if(!vis[i][j])
{
if(s[i][j]=='.')
{
dfs(i,j);
}
}
}
for(int i=n-1,j=0;j<m;j++)
{
if(!vis[i][j])
{
if(s[i][j]=='.')
{
ans=1;
dfs(i,j);
}
}
}
for(int j=m-1,i=0;i<n;i++)
{
if(!vis[i][j])
{
if(s[i][j]=='.')
{
ans=1;
dfs(i,j);
}
}
}//上面这四个for是用来判断四周有没有海洋,如果有把他们的vis赋值为1;这样在后面找湖泊的时候就不用看它们了 
for(int i=1;i<n-1;i++)
{
for(int j=1;j<m-1;j++)
{
if(!vis[i][j])
{
if(s[i][j]=='.')
{
ans=1;
dfs(i,j);
a[t].ant =ans;
a[t].x =i;
a[t].y =j;
t++;
}
}
}

if(t==0||t<=k)
 {
printf("0\n");
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
printf("%c",s[i][j]);
printf("\n"); 
}
 }
 else
 {
sort(a,a+t,cmp);
int l=0;
for(int j=t-1;j>=0;j--)
{
if(l>=t-k)
       break;
ex=a[j].x,ey=a[j].y;
l++;
       Dfs(ex,ey);
       sum+=a[j].ant ;
}
printf("%d\n",sum);
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
printf("%c",s[i][j]);
printf("\n"); 
}
 }
return 0;
}

原创粉丝点击