您的位置:首页 > 其它

【Codeforces Round 375 (Div 2) D】【简单dfs】Lakes in Berland

2016-10-04 10:18 435 查看
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.

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 55, M = 0, Z = 1e9 + 7, inf = 0x3f3f3f3f;
template <class T1, class T2>inline void gadd(T1 &a, T2 b) { a = (a + b) % Z; }
int n, m, k;
char s

;
int vis

;
int tim;
int num[N*N];
void dfs(int y, int x)
{
if (y<1 || y>n || x<1 || x>m)return;
if (s[y][x] == '*')return;
if (vis[y][x] != -1)return;
vis[y][x] = tim;
++num[tim];
dfs(y, x - 1);
dfs(y, x + 1);
dfs(y - 1, x);
dfs(y + 1, x);
}
int a[N*N];
bool fl[N*N];
bool cmp(int x, int y)
{
return num[x] < num[y];
}
int main()
{
while (~scanf("%d%d%d", &n, &m, &k))
{
for (int i = 1; i <= n; ++i)scanf("%s", s[i] + 1);
MS(vis, -1);
tim = 0;
for (int i = 1; i <= n; ++i)dfs(i, 1), dfs(i, m);
for (int j = 1; j <= m; ++j)dfs(1, j), dfs(n, j);
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)
{
if (s[i][j] == '.' && vis[i][j] == -1)
{
++tim;
num[tim] = 0;
dfs(i, j);
}
}
}
for (int i = 1; i <= tim; ++i)a[i] = i;
sort(a + 1, a + tim + 1, cmp);
k = tim - k;
MS(fl, 0);
int ans = 0;
for (int i = 1; i <= k; ++i)fl[a[i]] = 1, ans += num[a[i]];
printf("%d\n", ans);
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)if(vis[i][j] >= 1)
{
if (fl[vis[i][j]])s[i][j] = '*';
}
puts(s[i] + 1);
}
}
return 0;
}
/*
【题意】
给你一个图,四联通的'.'表示湖泊,湖泊联通块个数>=k
让你输出——
最小填充多少个格子,可以把所有湖泊的个数填充为k

【类型】
dfs

【分析】
先把边缘的海隔离化,然后dfs出每个联通块的size,最后贪心填充最小的

【时间复杂度&&优化】
O(nm)

*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息