zoukankan      html  css  js  c++  java
  • 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 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.

    /*
    不碰边界的连通块算是一个湖,给你要保留的湖的数目,一次填一个点,求填湖的最少次数
    sb爆搜,注意最后那个贪心选湖,还有那个边界的判断,比赛写错了
    */
    #include<iostream>
    #include<cstdio>
    #include<string>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    struct orz{
        int p;
        int sz;
    };
    orz lke[10005];
    int mp[105][105],vis[105][105],cnt,n,m,k,tot;
    bool ok[10005],sgn[10005];
    int dx[4] = {-1,0,1,0};
    int dy[4] = {0,1,0,-1};
    char cmd;
    bool jud(int y,int x){
        return y >= 1 && x >= 1 && y <= n && x <= m && !vis[y][x] && mp[y][x] == 1;
    }
    bool dfs(int y,int x){
        vis[y][x] = cnt;
        lke[cnt].sz++;
        int ny,nx;
        int gg = false;
        for(int i = 0;i < 4;i++){
            ny = y + dy[i];
            nx = x + dx[i];
            if(ny < 1 || ny > n || nx < 1 || nx > m) gg = true;
            if(jud(ny,nx)) if(!dfs(ny,nx)) gg = true;
        }
        if(gg) return false;
        else return true;
    }
    bool cmp(orz a,orz b){
        return a.sz < b.sz;
    }
    int main(){
        cin>>n>>m>>k;
        for(int i = 1;i <= n;i++){
            for(int j = 1;j <= m;j++){
                scanf("%c",&cmd);
                while(cmd != '*' && cmd != '.') scanf("%c",&cmd);
                if(cmd == '*') mp[i][j] = 2;
                else if(cmd == '.') mp[i][j] = 1;
            }
        }
        for(int i = 1;i <= n;i++){
            for(int j = 1;j <= m;j++){
                if(mp[i][j] == 1 && !vis[i][j]){
                    cnt++;
                    lke[cnt].p = cnt;
                    ok[cnt] = dfs(i,j);
                    if(ok[cnt]) tot++;
                }
            }
        }
        sort(lke+1,lke+1+cnt,cmp);
        int chs = 0,ans = 0;
        for(int i = 1;i <= cnt;i++){
            if(chs >= tot - k) break;
            if(ok[lke[i].p]){
                sgn[lke[i].p] = true;
                chs++;
                ans += lke[i].sz;
            }
            
        }
        cout<<ans<<endl;
        for(int i = 1;i <= n;i++){
            for(int j = 1;j <= m;j++){
                if(mp[i][j] == 2 || sgn[vis[i][j]]) cout<<"*";
                else cout<<".";
            }
            cout<<endl;
        }
        return 0;
    }
  • 相关阅读:
    .NET Core下的Socket示例.
    VS没办法调试,直接退出,报错:1. 使用调试生成配置或禁用调试选项“启用‘仅我的代码’”。。。
    2017年2月7日 今年第一天上班了
    .NET Core错误:The specified framework 'Microsoft.NETCore.App', version '1.0.0-rc2-3002702' was not found.
    KB2533623 下载
    Ajax Not Found,asp.net mvc 中
    JavaScript外部函数调用AngularJS的函数、$scope
    029医疗项目-模块三:药品供应商目录模块——供货商药品目录查询功能----------数据模型的分析(建表)
    028医疗项目-模块三:药品供应商目录模块——供货商药品目录查询功能----------需求分析
    50个查询系列-第10个查询:查询没有学全所有课的同学的学号、姓名;
  • 原文地址:https://www.cnblogs.com/hyfer/p/5929951.html
Copyright © 2011-2022 走看看