Codeforces #375(Div.2)D. Lakes in Berland【Bfs】

来源:互联网 发布:闲鱼申请淘宝介入几天 编辑:程序博客网 时间:2024/05/18 00:21

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 sizen × 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 beexactly k lakes in Berland. Note that the initial number of lakes on the map is not less thank.

Input

The first line of the input contains three integersn,m andk (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 containm 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 leastk 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 printm 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
***
***
***

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个,我们现在需要将一些内陆湖添成陆地,使得最终内陆湖的个数为K个,问最少填了几个格子,然后输出此时的图。


思路:


1、因为要的是内陆湖,那么边界上的湖是不算数的,所以我们最初的时候先将最外层的边界上的湖都去掉。


2、然后我们在剩下的图中Bfs统计内陆湖的个数,对应每个湖都染上不同的颜色。


3、然后我们对应每种颜色的湖都统计起来,排序之后去掉前contn(内陆湖的个数)-K个内陆湖即可。


Ac代码:

#include<stdio.h>#include<string.h>#include<queue>#include<algorithm>using namespace std;struct node2{    int num,val;}tmp[50*50*10*10];struct node{    int x,y;}now,nex;int n,m,k;char a[500][500];int vis[500][500];int vis2[500][500];int ans[50*50*10];int del[500*500];int fx[4]={0,0,1,-1};int fy[4]={1,-1,0,0};int cmp(node2 a,node2 b){    return a.val<b.val;}void Bfs(int x,int y,int col){    queue<node>s;    vis[x][y]=col;    now.x=x;    now.y=y;    s.push(now);    while(!s.empty())    {        now=s.front();        s.pop();        for(int i=0;i<4;i++)        {            nex.x=now.x+fx[i];            nex.y=now.y+fy[i];            if(vis[nex.x][nex.y]==0&&nex.x>=0&&nex.x<n&&nex.y>=0&&nex.y<m&&a[nex.x][nex.y]=='.')            {                vis[nex.x][nex.y]=col;                s.push(nex);            }        }    }}void Bfs2(int x,int y){    queue<node>s;    vis2[x][y]=1;    now.x=x;    now.y=y;    s.push(now);    while(!s.empty())    {        now=s.front();        s.pop();        for(int i=0;i<4;i++)        {            nex.x=now.x+fx[i];            nex.y=now.y+fy[i];            if(vis2[nex.x][nex.y]==0&&nex.x>=0&&nex.x<n&&nex.y>=0&&nex.y<m&&a[nex.x][nex.y]=='.')            {                vis2[nex.x][nex.y]=1;                a[nex.x][nex.y]='*';                s.push(nex);            }        }    }}int main(){    while(~scanf("%d%d%d",&n,&m,&k))    {        memset(vis2,0,sizeof(vis2));        memset(del,0,sizeof(del));        memset(ans,0,sizeof(ans));        memset(vis,0,sizeof(vis));        for(int i=0;i<n;i++)        {            scanf("%s",a[i]);        }        for(int i=0;i<n;i++)        {            for(int j=0;j<m;j++)            {                if(a[i][j]=='.'&&vis2[i][j]==0)                {                    if(i==0||i==n-1||j==0||j==m-1)                    {                        Bfs2(i,j);                    }                }            }        }        int cont=0;        for(int i=0;i<n;i++)        {            for(int j=0;j<m;j++)            {                if(a[i][j]=='.')                {                    if(i==0||i==n-1||j==0||j==m-1)continue;                    if(vis[i][j]==0)                    {                        cont++;                        Bfs(i,j,cont);                    }                }            }        }        for(int i=0;i<n;i++)        {            for(int j=0;j<m;j++)            {                if(vis[i][j]!=0)                {                    ans[vis[i][j]]++;                }            }        }        for(int i=1;i<=cont;i++)        {            tmp[i].num=i;            tmp[i].val=ans[i];        }        sort(tmp+1,tmp+cont+1,cmp);        int output=0;        for(int i=1;i<=cont-k;i++)        {            del[tmp[i].num]=1;            output+=tmp[i].val;        }        printf("%d\n",output);        for(int i=0;i<n;i++)        {            for(int j=0;j<m;j++)            {                if(a[i][j]=='.'&&del[vis[i][j]]==1&&vis[i][j]>0)                a[i][j]='*';            }        }        for(int i=0;i<n;i++)        {            for(int j=0;j<m;j++)            {                if(vis2[i][j]==1)printf(".");                else printf("%c",a[i][j]);            }            printf("\n");        }    }}/*5 4 0*****..***.***.*....*/



0 0
原创粉丝点击