• Codeforces Round #375 (Div. 2) D


    Description

    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.

    题意:k表示留下多少个池塘(注意如果和边界相连就不是池塘),那么我要把其他不要的池塘填满,最少花费多少

    解法:先dfs保存每个池塘的起点,大小,并将点'.'替换为其他字符(这里替换为B),按大小排序,从最小的开始填,每次填就从这个池塘的起点dfs,把这个池塘变为'*',那么剩下的B是不需要操作的,换成'.'就好

    #include<stdio.h>
    //#include<bits/stdc++.h>
    #include<string.h>
    #include<iostream>
    #include<math.h>
    #include<sstream>
    #include<set>
    #include<queue>
    #include<map>
    #include<vector>
    #include<algorithm>
    #include<limits.h>
    #define inf 0x3fffffff
    #define INF 0x3f3f3f3f
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define LL long long
    #define ULL unsigned long long
    using namespace std;
    int n,m,k;
    int num=0;
    char ma[200][200];
    int vis[200][200];
    int vis1[200][200];
    int flag=0;
    struct P
    {
        int x,y,num;
    }mp[10000];
    void dfs(int x,int y)
    {
        if(x<0||x>=n||y<0||y>=m)
        {
            flag=1;
            return;
        }
        if(ma[x][y]=='*')return;
        if(vis[x][y])return;
        vis[x][y]=1;
        if(ma[x][y]=='.')
        {
            num++;
            ma[x][y]='B';
        }
        dfs(x+1,y);
        dfs(x-1,y);
        dfs(x,y+1);
        dfs(x,y-1);
    }
    void dfs1(int x,int y)
    {
        if(x<0||x>=n||y<0||y>=m)
        {
            // flag=1;
            return;
        }
        if(ma[x][y]=='*')return;
        if(vis1[x][y])return;
        vis1[x][y]=1;
        if(ma[x][y]=='B')
        {
            ma[x][y]='*';
        }
        dfs1(x+1,y);
        dfs1(x-1,y);
        dfs1(x,y+1);
        dfs1(x,y-1);
    }
    bool cmd(P a,P b)
    {
        return a.num<b.num;
    }
    int main()
    {
    
        cin>>n>>m>>k;
        for(int i=0; i<n; i++)
            scanf("%s",ma[i]);
        int ans=0;
        for(int i=0; i<n; i++)
        {
            for(int k=0; k<m; k++)
            {
                //cout<<ma[i][k];
                if(ma[i][k]=='.')
                {
                    int px=i;
                    int py=k;
                    num=0;
                    flag=0;
                    dfs(i,k);
                    ans++;
                    if(flag==1)
                    {
                        ans--;
                    }
                    else
                    {
                        mp[ans].x=px;
                        mp[ans].y=py;
                        mp[ans].num=num;
                    }
                    //cout<<num<<endl;
                }
            }
        }
        int pos=0;
        sort(mp+1,mp+1+ans,cmd);
        for(int i=1; i<=ans-k; i++)
        {
            pos+=mp[i].num;
            dfs1(mp[i].x,mp[i].y);
        }
        printf("%d
    ",pos);
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                if(ma[i][j]=='B')
                {
                    //ma[i][j]='.';
                    printf(".");
                }
                else if(ma[i][j]=='*')
                {
                    printf("*");
                }
            }
            if(i<n-1)
            {
            puts("");
            }
        }
        return 0;
    }
    

      

  • 相关阅读:
    SQL UNION 和 UNION ALL 操作符
    JavaScript--验证码随机生成
    C#基础 out和ref
    远程连接身份验证错误,又找不到加密Oracle修正
    ASP.Net WebAPI -- 简单实现增删改查
    SpringBoot发送邮箱验证码
    VMware虚拟机安装Linux系统详细教程
    SpringBoot实现登陆拦截
    JavaScript 实例、构造函数、原型对象关系图
    JavaScript创建对象的几种方式总结
  • 原文地址:https://www.cnblogs.com/yinghualuowu/p/5929848.html
Copyright © 2020-2023  润新知