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

    水到极致了 
    但是比赛的时候把i写成0
    上限写死    脑子犯浑。。

    这个题明显dfs的话代码量会小一点,出于对并查集的熟悉还是先写了并查。。 代码炒鸡长而且丑  不过跑得飞快
    啊啊啊啊啊,还是太菜了啊啊啊啊啊

    #include <stdio.h>
    #include <iostream>
    #include <stack>
    #include <algorithm>
    #include <cstring>
    #include <vector>
    #include <cmath>
    #include<cctype>
    using namespace std;
    typedef long long ll;
    int n,m,k;
    char ch[60][60];
    int val[60][60];
    int par[3000];
    int cnt[3000];
    bool vis[3000];
    void init()
    {
        memset(ch,'.',sizeof(ch));
        //memset(vis,false,sizeof(vis));
        int tot = 0;
        for(int i=0;i<=n+1;i++)
        {
            for(int j=0;j<=m+1;j++)
            {
                val[i][j] = ++tot;
            }
        }
        for(int i=1;i<=tot;i++)
        {
            par[i] = i;
            cnt[i] = 1;
        }
    }
    int find(int x)
    {
        if(x==par[x]) return x;
        return par[x] = find(par[x]);
    }
    void unite(int x,int y)
    {
        x = find(x);
        y = find(y);
        if(x==y) return ;
        else{
            par[x] = y;
            cnt[y] += cnt[x];
        }
    }
    void make(int i,int j)
    {
        int dx[] = {0,0,1,-1};
        int dy[] = {1,-1,0,0};
        for(int k=0;k<4;k++)
        {
            if(ch[dx[k]+i][dy[k]+j]=='.')
                unite(val[i][j],val[dx[k]+i][dy[k]+j]);
        }
    }
    vector<int>v;
    bool cmp(int x,int y)
    {
        return cnt[x]<cnt[y];
    }
    int main()
    {
        scanf("%d%d%d",&n,&m,&k);
        init();
        for(int i=0;i<=n+1;i++)
        {
            unite(val[0][0],val[i][0]);
            unite(val[0][0],val[i][m+1]);
        }
        for(int i=0;i<=m+1;i++)
        {
            unite(val[0][0],val[0][i]);
            unite(val[0][0],val[n+1][i]);
        }
        for(int i=1;i<=n;i++)
        {
            scanf("%s",ch[i]+1);
            ch[i][m+1] = '.';
        }
        for(int i=1;i<=n;++i)
            for(int j=1;j<=m;j++)
            {
               if(ch[i][j]=='.') make(i,j);
            }
        int tt = 0;
        for(int i=1;i<=n;++i)
            for(int j=1;j<=m;j++)
            {
                int rt= find(val[i][j]);
                if(ch[i][j]=='.'&&find(val[0][0])!=rt)
                {
                    if(vis[rt]) continue;
                    else vis[rt] = true;
                    v.push_back(rt);
                    tt++;
                }
            }
        sort(v.begin(),v.end(),cmp);
        int len = v.size();
        int ans = 0;
        int st = 0;
        while(tt>k)
        {
            for(int i=st;i<len;i++)
            {
                int rt = find(v[i]);
                if(vis[rt])
                {
                    tt--;
                    vis[rt] = false;
                    for(int i=1;i<=n;++i)
                        for(int j=1;j<=m;j++)
                            if(find(val[i][j]) == rt) {ch[i][j] = '*';ans++;}
                    break;
                }
            }
            st++;
        }
        cout<<ans<<endl;
        for(int i=1;i<=n;++i)
        {
            for(int j=1;j<=m;j++)
            {
                putchar(ch[i][j]);
            }
            putchar('
    ');
        }
    
        return 0;
    }
    AC代码
  • 相关阅读:
    通用分页后台显示
    自定义的JSP标签
    Java反射机制
    Java虚拟机栈---本地方法栈
    XML建模实列
    XML解析与xml和Map集合的互转
    [离散数学]第二次作业
    [线性代数]2016.10.13作业
    [数字逻辑]第二次作业
    [线性代数]2016.9.26作业
  • 原文地址:https://www.cnblogs.com/Geek-xiyang/p/5930245.html
Copyright © 2011-2022 走看看