Codeforces-723D- Lakes in Berland(DFS)

来源:互联网 发布:你睡过几个男人 知乎 编辑:程序博客网 时间:2024/05/18 01:44
点击打开链接

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

Output

1
****
*..*
****
****
..**

Input

3 3 0
***
*.*
***

Output

1
***
***
***

#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespace std;int n,m,t,ans,ok;int l[4]={0,0,-1,1},r[4]={-1,1,0,0};int viso[101][101];char map[101][101];struct node{int x,y,z;//存储 湖波的位置数量 }d[10001];void dfs(int x,int y){if(x==0||y==0||x==n-1||y==m-1){//判定是否在边界 ok=0;}viso[x][y]=1;ans++;//点的数量 for(int i=0;i<4;i++){int xx=x+l[i],yy=y+r[i];if(xx>=0&&yy>=0&&xx<n&&yy<m&&!viso[xx][yy]&&map[xx][yy]=='.'){//深搜出"." dfs(xx,yy);}}}void DFS(int x,int y)//把所有不满足条件的"."变成"*"; {map[x][y]='*';for(int i=0;i<4;i++){int xx=x+l[i],yy=y+r[i];if(map[xx][yy]=='.'){DFS(xx,yy);}}}bool cmp(node i,node j)//比较出所有湖的面积大小 {return i.z>j.z;}int main(){scanf("%d %d %d",&n,&m,&t);for(int i=0;i<n;i++){scanf("%s",&map[i]);}int l=0;for(int i=0;i<n;i++){for(int j=0;j<m;j++){if(!viso[i][j]&&map[i][j]=='.'){//判定点并储存在结构体中 ok=1;ans=0;dfs(i,j);if(ok){d[l].x=i;d[l].y=j;d[l++].z=ans;}}}}sort(d,d+l,cmp);//排序 int sum=0;for(int i=t;i<l;i++){sum+=d[i].z;DFS(d[i].x,d[i].y);}printf("%d\n",sum);for(int i=0;i<n;i++){printf("%s\n",map[i]);}return 0;}


原创粉丝点击