zoukankan      html  css  js  c++  java
  • CF723D. Lakes in Berland[DFS floodfill]

    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.


    题意:有一些湖,定义见原题,填一些湖使得剩下k个湖


    DFS找湖

    把湖从小到大填就行了

    注意递归别调用错函数

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    #include <map>
    using namespace std;
    typedef long long ll;
    const int N=55;
    inline int read(){
        char c=getchar();int x=0,f=1;
        while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
        while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
        return x*f;
    }
    int n,m,k;
    char g[N][N];
    int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1};
    int vis[N][N],num[N*N],cc=0;
    struct lakes{
        int size,id;
    }lake[N*N];
    int cnt=0;
    bool cmp(lakes &a,lakes &b){
        return a.size<b.size;
    }
    void dfs(int x,int y,int id){//printf("dfs %d %d %d
    ",x,y,id);
        vis[x][y]=id;num[id]++;
        for(int i=0;i<4;i++){
            int nx=x+dx[i],ny=y+dy[i];
            if(nx<1||nx>n||ny<1||ny>m) continue;
            if(g[nx][ny]=='*'||vis[nx][ny]) continue;
            dfs(nx,ny,id);
        }
    }
    int ans=0;
    void fil(int x,int y,int id){//printf("fil %d %d %d
    ",x,y,id);
        g[x][y]='*';ans++;
        for(int i=0;i<4;i++){
            int nx=x+dx[i],ny=y+dy[i];
            if(nx<1||nx>n||ny<1||ny>m) continue;
            if(vis[nx][ny]==id&&g[nx][ny]=='.') fil(nx,ny,id);
        }
    }
    int main(){
        n=read();m=read();k=read();
        for(int i=1;i<=n;i++){
            scanf("%s",g[i]);
            for(int j=m;j>=1;j--) g[i][j]=g[i][j-1];
        }
        
        for(int i=1;i<=n;i++){
            if(!vis[i][1]&&g[i][1]=='.') dfs(i,1,++cc);
            if(!vis[i][m]&&g[i][m]=='.') dfs(i,m,++cc);
        }
        for(int j=1;j<=m;j++){
            if(!vis[1][j]&&g[1][j]=='.') dfs(1,j,++cc);
            if(!vis[n][j]&&g[n][j]=='.') dfs(n,j,++cc);
        }
        int sea=cc;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++){
                if(!vis[i][j]&&g[i][j]=='.')
                    dfs(i,j,++cc);
            }
        for(int i=sea+1;i<=cc;i++) {lake[++cnt].size=num[i];lake[cnt].id=i;}//printf("%d %d
    ",sea,cc);
        sort(lake+1,lake+1+cnt,cmp);
        //for(int i=1;i<=cnt;i++) printf("lake %d %d
    ",lake[i].id,lake[i].size);
        int t=cnt-k,p=1;//printf("t %d
    ",t);
        for(int z=1;z<=t;z++){
            int fin=0;
            for(int i=1;i<=n;i++){
                for(int j=1;j<=m;j++)
                    if(lake[p].id==vis[i][j]){fil(i,j,lake[p++].id);fin=1;break;}
                if(fin) break;
            }
        }
        printf("%d
    ",ans);
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++) printf("%c",g[i][j]);
            if(i!=n) putchar('
    ');
        }
        
    }
  • 相关阅读:
    android通过Canvas和Paint截取无锯齿圆形图片
    【转】mysql的cardinality异常,导致索引不可用
    mysql索引无效且sending data耗时巨大原因分析
    linux shell脚本通过参数名传递参数值
    git日志输出格式及两个版本之间差异列表
    jenkins结合ansible用shell实现自动化部署和回滚
    Linux下cp -rf总是提示覆盖的解决办法
    jenkins集成ansible注意事项Failed to connect to the host via ssh.
    ansible操作远程服务器报Error: ansible requires the stdlib json or simplejson module, neither was found!
    利用ssh-copy-id无需密码登录远程服务器
  • 原文地址:https://www.cnblogs.com/candy99/p/5930708.html
Copyright © 2011-2022 走看看