Codeforces Round #375 (Div. 2)

题目链接: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.

 再也没有比AC题更快乐的了!

思路: 第一遍dfs找相连通块,并记录每个Lake的大小count1,并把count1放到优先队列里,第二次dfs给每个Lake中的ceil标记为count1,

优先队列里从小到大,先出小的值cur,然后dfs染一个大小为cur的Lake.直到等于k.

思路挺简单,但手误好多,我写的时候竟然把数组int to[4][2]写成to[8][2]竟然还debug了半小时! 然后就是触碰到边少了个flag.

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <algorithm>
  4 #include <cstring>
  5 #include <map>
  6 #include <queue>
  7 using namespace std;
  8 const int maxn = 55;
  9 char s[maxn][maxn];
 10 int vis[maxn][maxn];
 11 int to[4][2] = {1,0,-1,0,0,1,0,-1};
 12 int n,m,k;
 13 priority_queue<int, vector<int>, greater<int> > q;
 14 int flag = 0;
 15 void dfs(int x,int y,int &count1)
 16 {
 17     if(x<0||x>=n||y<0||y>=m)
 18     {
 19         flag = 1;
 20         return;
 21     }
 22     if(vis[x][y]!=0||s[x][y]=='*') return;
 23     vis[x][y] = -1;
 24     count1++;
 25   //  printf("%d
",count1);
 26     for(int i=0;i<4;i++)
 27     {
 28         int nex = x+to[i][0];
 29         int ney = y+to[i][1];
 30         dfs(nex,ney,count1);
 31     }
 32 }
 33 void dfs2(int x,int y)
 34 {
 35     if(x<0||x>=n||y<0||y>=m)
 36     {
 37         return;
 38     }
 39     if(vis[x][y]==-1||s[x][y]=='*') return;
 40     vis[x][y] = -1;
 41     s[x][y] = '*';
 42     for(int i=0;i<4;i++)
 43     {
 44         int nex = x+to[i][0];
 45         int ney = y+to[i][1];
 46         dfs2(nex,ney);
 47     }
 48 }
 49 void dfs1(int x,int y,int count1)
 50 {
 51     if(x<0||x>=n||y<0||y>=m)
 52     {
 53         return;
 54     }
 55     if(vis[x][y]>0||s[x][y]=='*') return;
 56     vis[x][y] = count1;
 57     for(int i=0;i<4;i++)
 58     {
 59         int nex = x+to[i][0];
 60         int ney = y+to[i][1];
 61         dfs1(nex,ney,count1);
 62     }
 63 }
 64 int main()
 65 {
 66     cin>>n>>m>>k;
 67     for(int i=0;i<n;i++)
 68     {
 69         scanf("%s",s[i]);
 70     }
 71     memset(vis,0,sizeof(vis));
 72     for(int i=0;i<n;i++)
 73     {
 74         for(int j=0;j<m;j++)
 75         {
 76             if(!vis[i][j]&&s[i][j]=='.')
 77             {
 78                 flag = 0;
 79                 int count1 = 0;
 80                 dfs(i,j,count1);
 81                 if(count1>0&&!flag)   //前几次写这列少加了flag的判断
 82                 {
 83                     q.push(count1);
 84                     dfs1(i,j,count1);
 85                 }
 86             }
 87         }
 88     }
 89     int ans = 0;
 90   //  printf("哈哈
");
 91   //  printf("%d
",q.size());
 92     while(q.size()>k)
 93     {
 94         int cur = q.top();
 95         q.pop();
 96         flag = 0;
 97         ans += cur;
 98         for(int i=0;i<n&&!flag;i++)
 99         {
100             for(int j=0;j<m&&!flag;j++)
101             {
102                 if(s[i][j]=='.'&&vis[i][j]==cur)
103                 {
104                     dfs2(i,j);
105                     flag = 1;
106                 }
107             }
108         }
109     }
110     printf("%d
",ans);
111     for(int i=0;i<n;i++) printf("%s
",s[i]);
112     return 0;
113 }
原文地址:https://www.cnblogs.com/littlepear/p/5929962.html