# Codeforces Round #375 (Div. 2) D.Lakes in Berland (dfs + 贪心)

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.

dfs把地图上的湖泊染色，从小到大填成陆地直到符合条件。

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 110;
char mp[maxn][maxn];
int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1};
int vis[maxn][maxn];
int cnt[100086];
int n, m, k;

void dfs(int x, int y, int id) {
if(mp[x][y] == '*') return;
if(vis[x][y]) return; vis[x][y] = id;
for(int i = 0; i < 4; i++) {
int xx = x + dx[i], yy = y + dy[i];
if(mp[x + dx[i]][y + dy[i]] == '.' && xx >= 1 && xx <= n && yy >= 1 && yy <= m)
dfs(x + dx[i], y + dy[i], id);
}
}

int main() {
while(~scanf("%d %d %d", &n, &m, &k)) {
memset(vis, 0, sizeof(vis));
for(int i = 1; i <= n; i++) {
scanf("%s", mp[i] + 1);
}
int cnt = 0;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
if(!vis[i][j] && mp[i][j] == '.')
++cnt;
dfs(i, j, cnt);
}
}
/*for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
printf("%02d ", vis[i][j]);
}puts("");
}*/
int cnt2 = 0;
map<int, int> mp3;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
if(i == 1 || i == n || j == 1 || j == m) {
mp3[vis[i][j]] = -1;
}
else if(mp3[vis[i][j]] >= 0) mp3[vis[i][j]]++;
}
}
vector< pair<int, int> > vec;
for(auto tmp:mp3) {
if(tmp.second > 0) {
//printf("check %d %d\n", tmp.first, tmp.second);
cnt2++;
vec.emplace_back(tmp.second, tmp.first);
}
}
sort(vec.begin(), vec.end());
int ans = 0;
int need = cnt2 - k;
map<int, int> mp2;
for(int i = 0; i < need; i++) {
ans += vec[i].first;
mp2[vec[i].second] = 1;
}
printf("%d\n", ans);
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
if(mp2[vis[i][j]] == 1) {
printf("*");
}
else printf("%c", mp[i][j]);
}puts("");
}
}
}

posted @ 2016-10-03 23:00  MartinEden  阅读(125)  评论(0编辑  收藏