Codeforces Round #375 (Div. 2) D. Lakes in Berland dfs

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<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
const int N=2e5+10,M=4e6+10,inf=1e9+10,mod=1e9+7;
const ll INF=1e18+10;
int n,m,k;
char mp[60][60];
int xx[4]={1,0,-1,0};
int yy[4]={0,-1,0,1};
int vis[60][60];
int p[N];
struct is
{
    int si;
    int pos;
    /*bool operator <(const is &b)const
    {
        if(p[pos]!=p[b.pos])
        return p[pos]<p[b.pos];
        return si>b.si;
    }*/
}a[N];
int cmp(is a,is b)
{
    if(p[a.pos]!=p[b.pos])
        return p[a.pos]<p[b.pos];
    return a.si>b.si;
}
int check(int x,int y)
{
    if(x>=n||x<0||y>=m||y<0) return 0;
    return 1;
}
void dfs(int x,int y,int z)
{
    vis[x][y]=z;
    for(int i=0;i<4;i++)
    {
        int xxx=x+xx[i];
        int yyy=y+yy[i];
        if(check(xxx,yyy)&&mp[xxx][yyy]=='.'&&!vis[xxx][yyy])
        {
            dfs(xxx,yyy,z);
        }
    }
}
int main()
{
    scanf("%d%d%d",&n,&m,&k);
    for(int i=0;i<n;i++)
        scanf("%s",mp[i]);
    int flag=1;
    for(int i=0;i<n;i++)
    {
        for(int t=0;t<m;t++)
        {
            if(mp[i][t]=='.'&&!vis[i][t])
                dfs(i,t,flag++);
        }
    }
    for(int i=1;i<flag;i++)
    a[i].pos=i,a[i].si=0;
    for(int i=0;i<n;i++)
    {
        for(int t=0;t<m;t++)
        {
            if(vis[i][t]>0)
            {
                a[vis[i][t]].si++;
                if(i==0||i==n-1||t==0||t==m-1)
                    p[vis[i][t]]=1;
            }
        }
    }
    sort(a+1,a+flag,cmp);
    int ans=0;
    for(int i=1;i<flag;i++)
    {
        if(p[a[i].pos]==0&&i>k)
            ans+=a[i].si;
    }
    for(int i=1;i<=k;i++)
        p[a[i].pos]=1;
    printf("%d\n",ans);
    for(int i=0;i<n;i++)
    {
        for(int t=0;t<m;t++)
        {
            if(vis[i][t]>0)
            {
                if(p[vis[i][t]])
                printf(".");
                else
                printf("*");
            }
            else
                printf("*");
        }
        printf("\n");
    }
    return 0;
}
复制代码

 

posted @   jhz033  阅读(116)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
点击右上角即可分享
微信分享提示