[原创]codeforces 723D. Lakes in Berland [DFS]【】

2016-10-04 17:34:41 Tabris_ 阅读数:400


博客爬取于2020-06-14 22:43:07
以下为正文

版权声明:本文为Tabris原创文章,未经博主允许不得私自转载。
https://blog.csdn.net/qq_33184171/article/details/52734656


题目链接:http://codeforces.com/contest/723/problem/D
----------------------------------------.
D. Lakes in Berland
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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个湖 所以要填上一些湖 问最小花费(填一格花费1)

解题思路:
就是无脑DFS
先把海搜索一遍 然后开始搜索湖 并染色 (染色拿vis数组染色就行)
拿一个结构体记录每个颜色的湖的大小 按照湖的大小从小到大排下序 最小的开始填湖 就能得到最小花费了

/**************************/
打CF的时候无脑WA7 过后重敲一发 居然1发就AC了。。。思路一模一样。。。。 GG。。

附本题代码
--------------------------.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# include<bits/stdc++.h>
using namespace std;

# define lalal puts("*****");
# define abs(a) (a)>0?(a):-(a)
# define INF 0x1f1f1f1f
# define pb push_back

typedef long long int LL;
/***********************************/

struct node
{
int s,c;
} num[2555];
int cmp(node A,node B)
{
return A.s<B.s;
}

char a[55][55];
int vis[55][55];
int fx[4]= {0,0,1,-1};
int fy[4]= {1,-1,0,0};
int n,m,k;

void dfs(int x,int y,int &color,int &step)
{
step++,vis[x][y]=color;

int xx,yy;
for(int i=0;i<4;i++)
{
xx=x+fx[i];
yy=y+fy[i];
if(xx>0&&xx<=n&&yy>0&&yy<=m&&!vis[xx][yy]&&a[xx][yy]=='.')
dfs(xx,yy,color,step);
}
}

int main()
{
while(~scanf("%d %d %d",&n,&m,&k))
{
for(int i=1; i<=n; i++) for(int j=1; j<=m; j++) vis[i][j]=0;

for(int i=1; i<=n; i++) scanf("%s",a[i]+1);

int color=-1,step=0;
for(int i=1; i<=n; i++)
{
if(!vis[i][1]&&a[i][1]=='.')
dfs(i,1,color,step);
if(!vis[i][m]&&a[i][m]=='.')
dfs(i,m,color,step);
}
for(int j=1; j<=m; j++)
{
if(!vis[1][j]&&a[1][j]=='.')
dfs(1,j,color,step);
if(!vis[n][j]&&a[n][j]=='.')
dfs(n,j,color,step);
}

int len = 0; color=0;
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
{
if(!vis[i][j]&&a[i][j]=='.')
step=0,dfs(i,j,++color,step),num[len].s=step,num[len++].c=color;
}

int sum=0;
sort(num,num+len,cmp);
for(int u=0; u<len-k; u++)
{
sum+=num[u].s;
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
{
if(vis[i][j]==num[u].c)
a[i][j]='*';
}
}

printf("%d\n",sum);
for(int i=1;i<=n;i++) puts(a[i]+1);
}
return 0;
}