zoukankan      html  css  js  c++  java
  • codeforces 723D: Lakes in Berland

    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 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.

     

    正解:BFS

    解题报告:

      根据题目意思,先把海洋部分找出来,标记一下(BFS即可)。然后对于所有的处在内部的湖,BFS求出每个湖的面积,并且按湖的面积排序,优先把面积小的湖填掉。

      结果一直RE+WA的原因居然是BFS的时候,我的标记经过的数组是一个萎的,我是在从队首取出时才标记的,事实上入队的时候就应该考虑!注意细节!

     1 //It is made by jump~
     2 #include <iostream>
     3 #include <cstdlib>
     4 #include <cstring>
     5 #include <cstdio>
     6 #include <cmath>
     7 #include <algorithm>
     8 #include <ctime>
     9 #include <vector>
    10 #include <queue>
    11 #include <map>
    12 #include <set>
    13 using namespace std;
    14 typedef long long LL;
    15 const int inf = (1<<30);
    16 const int MAXN = 100;
    17 const int MAXL = 1000011;
    18 const int MOD = 1000000;
    19 int n,m,k,cnt,ans;
    20 int a[MAXN][MAXN];
    21 bool vis[MAXN][MAXN];
    22 int head,tail,dui[MAXL][2];
    23 int yi[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
    24 struct lake{
    25     int val;
    26     int x,y;
    27 }hu[MAXN*MAXN];
    28 inline bool cmp(lake q,lake qq){ return q.val<qq.val; }
    29 inline int getint()
    30 {
    31     int w=0,q=0; char c=getchar();
    32     while((c<'0' || c>'9') && c!='-') c=getchar(); if(c=='-') q=1,c=getchar(); 
    33     while (c>='0' && c<='9') w=w*10+c-'0', c=getchar(); return q ? -w : w;
    34 }
    35 
    36 inline void get_ocean(int inx,int iny){
    37     head=tail=0; dui[++tail][0]=inx; dui[tail][1]=iny; vis[inx][iny]=1; 
    38     int ux,uy,nowx,nowy;
    39     while(head!=tail) {
    40     head++; head%=MOD; ux=dui[head][0]; uy=dui[head][1];
    41     for(int i=0;i<4;i++) {
    42         nowx=ux+yi[i][0]; nowy=uy+yi[i][1]; if(nowx<=0 || nowy<=0 || nowx>n || nowy>m) continue; 
    43         if(!a[nowx][nowy]) continue; if(vis[nowx][nowy]) continue; 
    44         tail++; tail%=MOD; dui[tail][0]=nowx; dui[tail][1]=nowy; vis[nowx][nowy]=1;
    45     }
    46     }
    47 }
    48 
    49 inline int BFS(int inx,int iny){
    50     head=tail=0; dui[++tail][0]=inx; dui[tail][1]=iny; vis[inx][iny]=1;
    51     int ux,uy,nowx,nowy,total=0;
    52     while(head!=tail) {
    53     head++; head%=MOD; ux=dui[head][0]; uy=dui[head][1]; 
    54     total++;
    55     for(int i=0;i<4;i++) {
    56         nowx=ux+yi[i][0]; nowy=uy+yi[i][1]; if(nowx<=0 || nowy<=0 || nowx>n || nowy>m) continue; 
    57         if(!a[nowx][nowy]) continue; if(vis[nowx][nowy]) continue; 
    58         tail++; tail%=MOD; dui[tail][0]=nowx; dui[tail][1]=nowy; vis[nowx][nowy]=1;
    59     }
    60     }
    61     return total;
    62 }
    63 
    64 inline void change(int inx,int iny){
    65     head=tail=0; dui[++tail][0]=inx; dui[tail][1]=iny; vis[inx][iny]=1;
    66     int ux,uy,nowx,nowy;
    67     while(head!=tail) {
    68     head++; head%=MOD; ux=dui[head][0]; uy=dui[head][1]; a[ux][uy]=0;
    69     for(int i=0;i<4;i++) {
    70         nowx=ux+yi[i][0]; nowy=uy+yi[i][1]; if(nowx<=0 || nowy<=0 || nowx>n || nowy>m) continue; 
    71         if(!a[nowx][nowy]) continue; if(vis[nowx][nowy]) continue;
    72         tail++; tail%=MOD; dui[tail][0]=nowx; dui[tail][1]=nowy; vis[nowx][nowy]=1;
    73     }
    74     }
    75 }
    76 
    77 inline void work(){
    78     n=getint(); m=getint(); k=getint(); char c;
    79     for(int i=1;i<=n;i++) for(int j=1;j<=m;j++)  { c=getchar(); while(c!='*' && c!='.') c=getchar(); if(c=='.') a[i][j]=1; }
    80     for(int i=1;i<=n;i++) { if(!vis[i][1] && a[i][1]) get_ocean(i,1); if(!vis[i][m] && a[i][m]) get_ocean(i,m); }
    81     for(int i=2;i<m;i++) { if(!vis[1][i] && a[1][i]) get_ocean(1,i); if(!vis[n][i] && a[n][i]) get_ocean(n,i); }
    82     for(int i=2;i<n;i++) for(int j=2;j<m;j++) if(!vis[i][j] && a[i][j]) { hu[++cnt].val=BFS(i,j); hu[cnt].x=i; hu[cnt].y=j; }
    83     memset(vis,0,sizeof(vis)); sort(hu+1,hu+cnt+1,cmp);
    84     for(int i=1;i<=cnt-k;i++) { ans+=hu[i].val; change(hu[i].x,hu[i].y); }
    85     printf("%d
    ",ans);
    86     for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) if(a[i][j]) printf("."); else printf("*"); printf("
    "); }
    87 }
    88 
    89 int main()
    90 {
    91     work();
    92     return 0;
    93 }
  • 相关阅读:
    IIS配置ThinkPHP重写
    Redis安装
    ubuntu学习笔记
    PHP连接MySQL数据库SELinux中一些setsebool的用法
    centos+frp
    centos7.6安装nginx
    在CentOS 7-8上安装PHP 8.0
    centos7安装.net5.0(core)
    阿里云服务器配置清单
    阿里云服务器centos7.6安装mysql8.0.23
  • 原文地址:https://www.cnblogs.com/ljh2000-jump/p/5934370.html
Copyright © 2011-2022 走看看