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

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

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值