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

计算数量时,把变量放在外面。。。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
using namespace std;
const int N = 600;
char str[N][N];
int vis[N][N];
struct node
{
    int x, y, num;
}p[N*N];
int n, m, k, flag, num1;
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
void dfs(int x,int y)
{
    vis[x][y]=1;
    num1++;
    if(x==0||x==n-1||y==0||y==m-1)
        flag=-1;
    for(int i=0;i<4;i++)
    {
        int a=x+dir[i][0],b=y+dir[i][1];
        if(vis[a][b]||str[a][b]=='*')
            continue;
        if(a>=0&&a<=n-1&&b>=0&&b<=m-1&&str[a][b]=='.')
        {
            dfs(a,b);
        }
    }
    return ;
}
int cmp(node A,node B)
{
    return A.num<B.num;
}
void fil(int x,int y)
{
    str[x][y]='*';
    for(int i=0;i<4;i++)
    {
        int a=x+dir[i][0],b=y+dir[i][1];
        if(a>0&&a<n-1&&b>0&&b<m-1&&str[a][b]=='.')
        {
            str[a][b]='*';
            fil(a,b);
        }
    }
    return ;
}

int main()
{
    while(scanf("%d %d %d", &n, &m, &k)!=EOF)
    {
        for(int i=0;i<n;i++)
        {
            scanf("%s",str[i]);
        }
        int cnt=0;
        memset(vis,0,sizeof(vis));
        memset(p,0,sizeof(p));
        for(int i=1;i<n-1;i++)
        {
            for(int j=1;j<m-1;j++)
            {
                if(str[i][j]=='.'&&vis[i][j]==0)
                {
                    flag=1;
                    num1=0;
                    dfs(i,j);
                    if(flag!=-1)
                    {
                        p[cnt].x=i,p[cnt].y=j,p[cnt].num=num1;
                        cnt++;
                    }
                }
            }
        }
        if(cnt>k)
        {
            sort(p,p+cnt,cmp);
            int cnt2=0, sum=0;
            while(cnt>k)
            {
                fil(p[cnt2].x,p[cnt2].y);
                sum+=p[cnt2].num;
                cnt2++;
                cnt--;
            }
            printf("%d\n",sum);
            for(int i=0;i<n;i++)
            {
                printf("%s\n",str[i]);
            }
        }
        else
        {
            printf("0\n");
            for(int i=0;i<n;i++)
            {
                printf("%s\n",str[i]);
            }
        }
    }
    return 0;
}

cf 723D Lakes in Berland(dfs)相关推荐

  1. CodeForces - 723D Lakes in Berland dfs

    Description The map of Berland is a rectangle of the size n × m, which consists of cells of size 1 × ...

  2. codeforces#375(div.2)723D - Lakes in Berland dfs+bfs

    题意:给你一张n×m的图,点代表水,星代表地,当水连在一块时,称为湖,但是,若湖中有水在边界上,则不算它是湖,现在要求你去掉x个湖,并用地将其填上,使原图剩下k个湖,输出你最少需要多少个单位的地,并且 ...

  3. Codeforeces - 723D -Lakes in Berland

    Codeforeces - 723D -Lakes in Berland time limit per test2 seconds memory limit per test256 megabytes ...

  4. CF723D. Lakes in Berland[DFS floodfill]

    D. Lakes in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  5. Codeforces 723D. Lakes in Berland

    解题思路: 1.dfs所有的水,顺便计数大小并判断是不是湖. 2.如果是湖,将大小和坐标存下来. 3.对湖按大小从小到大排序. 4.dfs前(湖的数量-k)个湖,用*填充这些湖. 代码: #inclu ...

  6. 【29.70%】【codeforces 723D】Lakes in Berland

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  7. cf723d Lakes in Berland

    The map of Berland is a rectangle of the size n × m, which consists of cells of size 1 × 1. Each cel ...

  8. codeforces723 D. Lakes in Berland(并查集)

    题目链接:codeforces723 D. Lakes in Berland 参考博客:http://www.cnblogs.com/Geek-xiyang/p/5930245.html 1 #inc ...

  9. 【Codeforces 723D】Lakes in Berland (dfs)

    海洋包围的小岛,岛内的有湖,'.'代表水,'*'代表陆地,给出的n*m的地图里至少有k个湖,求填掉面积尽量少的水,使得湖的数量正好为k. dfs找出所有水联通块,判断一下是否是湖(海水区非湖).将湖按 ...

最新文章

  1. Security issue about static code checking
  2. Git查看、删除、重命名远程分支和tag【转】
  3. Numpy中的random模块中的seed方法的作用
  4. 网易云课堂计算机体系,计算机系统结构 (三) CPU及其结构分析
  5. C# 发送邮件的记录(qq,126,Gmail)
  6. 543. 二叉树的直径 golang
  7. np.random.get_state()
  8. 安装完补丁后是否需要服务器重新启动
  9. string对象中去掉标点符号
  10. 基于JAVA+Servlet+JSP+MYSQL的保险管理系统
  11. Linux学习笔记013---CentOs7中vsftpd的安装和卸载
  12. mac应用 已损坏,打不开.你应该将它移到废纸篓
  13. 零基础带你玩转微信小程序--小程序的基础和安装
  14. 硬盘格式化了怎么恢复数据
  15. Stefan's Nutch Documentation
  16. 第3章第32节:图形的应用:使用图形表达并列关系的内容 [PowerPoint精美幻灯片实战教程]
  17. 如何在Dev-c++中打c语音的代码
  18. MYSQL的sqlca详解_sql数据库如何使用
  19. 机器视觉——光源介绍
  20. Win10下安装MySQL

热门文章

  1. Reliable Evaluation of Adversarial Robustness with an Ensemble of Diverse Parameter-free attack
  2. 贵州省黔南布依族苗族自治州谷歌卫星地图下载
  3. 一位程序员的爱情表白
  4. Chrome游览器Google商店下载crx文件
  5. 数据仓库-数据仓库主要特征
  6. 昆明视频监控建设行之有效 明年底实现百分百覆盖
  7. 女性网站在中国将大有可为
  8. Dom生成Xml和解析Xml
  9. 二分类交叉熵,多分类交叉熵,focal loss
  10. Java筑基三《代理模式》