您的位置:首页 > 其它

F - Lakes in Berland

2017-08-04 18:42 375 查看
点击打开链接

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 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.

#include<stdio.h>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m,k,ans,t;
int b[55][55];
struct stu
{
int geshu;
int x,y;
}c[2505];
int fx[4]={0,0,-1,1},fy[4]={1,-1,0,0};
char a[55][55];
bool cmp(stu d,stu e )
{
return d.geshu<e.geshu;
}
void dfs1(int x,int y)
{
b[x][y]=1;
int xx,yy,i;
for(i=0;i<4;i++)
{
xx=x+fx[i];
yy=y+fy[i];
if(xx>=0&&xx<n&&yy>=0&&yy<m&&!b[xx][yy]&&a[xx][yy]=='.')
{
if(xx==0||xx==n-1||yy==0||yy==m-1||t!=0)
{
t++;
if(t==1)
{
c[ans].geshu=0;   //若是连接边缘的海且t==1(防止重复减去),则减去。
ans--;
}
dfs1(xx,yy);
}
else
{
c[ans].geshu++;
dfs1(xx,yy);
}
}
}
}
void dfs2(int x,int y)
{
b[x][y]=2;
a[x][y]='*';
int xx,yy,i;
for(i=0;i<4;i++)
{
xx=x+fx[i];
yy=y+fy[i];
if(xx>=0&&xx<n&&yy>=0&&yy<m&&b[xx][yy]==1&&a[xx][yy]=='.')
dfs2(xx,yy);
}
}
int main()
{
int i,j;
while(~scanf("%d %d %d",&n,&m,&k))
{
for(i=0;i<n;i++)
scanf("%s",a[i]);
memset(b,0,sizeof(b));
memset(c,0,sizeof(c));
ans=0;
for(i=1;i<n-1;i++)
for(j=1;j<m-1;j++)
if(!b[i][j]&&a[i][j]=='.')
{
t=0;
ans++;
c[ans].geshu++;
c[ans].x=i;   //记下此湖泊的第一个"."的位置,之后填土时可直接调用。
c[ans].y=j;
dfs1(i,j);
}
int sum=0;
if(ans==k)
{
printf("0\n");
for(i=0;i<n;i++)
printf("%s\n",a[i]);
}
else
{
sort(c+1,c+ans+1,cmp);
for(i=1;i<=ans-k;i++)
{
sum+=c[i].geshu;
dfs2(c[i].x,c[i].y);	   //将多余湖泊填土。
}
printf("%d\n",sum);
for(i=0;i<n;i++)
printf("%s\n",a[i]);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: