CodeForces - 723D Lakes in Berland dfs

 

Lakes in Berland

 CodeForces - 723D

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

思路:

先用一个dfs把外围的海挑出来,对于内部的水域,我们用一个num数组存该点对应的水域的格子个数,并把这个数存在数组a中最后用来消除要填的水域,最后用一个reset函数填;

写的很麻烦,用了三个dfs(弱鸡大哭),不过终于算是写出来了

代码:

#include<bits/stdc++.h>
using namespace std;
#define N 55
#define mem(a) memset(a,0,sizeof(a))
int n,m,s,cnt,ans = 0,num[N][N],a[2505];
char form[N][N];
bool vis[N][N];//用来记录走过的格子
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
bool check(int x,int y)
{
    if (x >= 0 && x < n && y >= 0 && y < m)
        return 1;
    return 0;
}
void dfs1(int x,int y)
{
    form[x][y] = '?';
    for (int i = 0;i < 4;i ++)
    {
        int ex = x + dx[i],ey = y + dy[i];
        if (check(ex,ey) && form[ex][ey] == '.')
            dfs1(ex,ey);
    }
}
void dfs(int x,int y)
{
    form[x][y] = '!';
    cnt ++;
    vis[x][y] = 1;
    for (int i = 0;i < 4;i ++)
    {
        int ex = x + dx[i],ey = y + dy[i];
        if (check(ex,ey) && form[ex][ey] == '.')
            dfs(ex,ey);
    }
}
void make()
{
    for (int i = 0;i < n;i ++)
        for (int j = 0;j < m;j ++)
            if (vis[i][j])
                num[i][j] = cnt;
}
void reset(int x,int y)
{
    form[x][y] = '*';
    ans ++;
    for (int i = 0;i < 4;i ++)
    {
        int ex = x + dx[i],ey = y + dy[i];
        if (check(ex,ey) && form[ex][ey] == '!')
            reset(ex,ey);
    }
}
int main()
{
    scanf("%d %d %d",&n,&m,&s);
    for (int i = 0;i < n;i ++)
        scanf("%s",form[i]);
    for (int i = 0;i < n;i ++)
        for (int j = 0;j < m;j ++)
            if ((!i || !j || i == n - 1 || j == m - 1) && form[i][j] == '.')
                dfs1(i,j);
    mem(num);
    int pos = 0;
    for (int i = 0;i < n;i ++)
        for (int j = 0;j < m;j ++)
        {
            if (form[i][j] == '.')
            {
                mem(vis);
                cnt = 0;
                dfs(i,j);
                make();
                a[pos ++] = cnt;
            }
        }
    if (s >= pos)
    {
        printf("0\n");
        for (int i = 0;i < n;i ++)
        {
            for (int j = 0;j < m;j ++)
            {
                if (form[i][j] == '?' || form[i][j] == '!')
                    printf(".");
                else printf("*");
            }
            putchar('\n');
        }
        return 0;
    }
    sort(a,a + pos);
    for (int i = 0;i < pos - s;i ++)
    {
        bool flag = 0;
        for (int j = 0;j < n;j ++)
        {
            for (int k = 0;k < m;k ++)
            {
                if (form[j][k] == '!' && num[j][k] == a[i])
                {
                    reset(j,k);
                    flag = 1;
                    break;
                }
            }
            if (flag) break;
        }

    }
    printf("%d\n",ans);
    for (int i = 0;i < n;i ++)
    {
        for (int j = 0;j < m;j ++)
        {
            if (form[i][j] == '!' || form[i][j] == '?')
                printf(".");
            else
                printf("%c",form[i][j]);
        }
        putchar('\n');
    }
    return 0;
}

 

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READme.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值