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.

Sample Input

Input
5 4 1
****
*..*
****
**.*
..**
Output
1
****
*..*
****
****
..**
Input
3 3 0
***
*.*
***
Output
1
***
***
***

Hint

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.


題意:給出 n,m 和 k 代表 n*m 的一個地圖,'*' 表示陸地,'.' 表示水泊,如果水泊在邊界則是靠着大海,不算是湖,要求剩 k 個湖。問最小的填充個數,和輸出出最後的地圖情況。

DFS,對於每個符合條件的湖泊都進行記錄並排序,從小到大挨着填充。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

char maps[55][55];
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
bool vis[55][55];

struct node
{
    int num;
    int x, y;
};
node pla[3000];

int cmp(node a, node b)
{
    return a.num < b.num;
}

int num;
bool flag;
int n, m, k;

void DFS(int x, int y)
{
    if (vis[x][y] || maps[x][y] == '*')
        return;
    printf("%d %d\n", x+1, y+1);
    num++;
    vis[x][y] = true;

    for (int i = 0; i < 4; ++i)
    {
        int nx = x + dx[i];
        int ny = y + dy[i];
        if (nx < 0 || nx > n || ny < 0 || ny > m)///排除掉邊界,即海邊
        {
            num = 0;
            flag = false;
            continue;
        }
        if (maps[nx][ny] == '*' || vis[nx][ny])
            continue;
        DFS(nx, ny);
    }
}

void Fill(int x, int y)
{
    maps[x][y] = '*';
    for (int i = 0; i < 4; ++i)
    {
        int nx = x + dx[i];
        int ny = y + dy[i];
        if (maps[nx][ny] == '*')
            continue;
        Fill(nx, ny);
    }
}

int main()
{
    scanf("%d %d %d", &n ,&m, &k);
    int cnt = 0;
    int ans = 0;

    memset(vis, false, sizeof(vis));
    for (int i = 0; i < n; ++i)
        scanf("%s", maps[i]);
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < m; ++j)
        {
            num = 0;
            flag = true;
            if (!vis[i][j] && maps[i][j] == '.')
            {
                DFS(i, j);
                if (flag)
                {
                    pla[cnt].num = num;
                    pla[cnt].x = i;
                    pla[cnt].y = j;
                    cnt++;
                }
            }
        }
    }
    sort(pla, pla + cnt, cmp);
    for (int i = 0; i < cnt - k; ++i)
        ans += pla[i].num;
    printf("%d\n", ans);

    for (int i = 0; i < cnt - k; ++i)
        Fill(pla[i].x, pla[i].y);
    for (int i = 0; i < n; ++i)
        printf("%s\n", maps[i]);
    return 0;
}


發佈了158 篇原創文章 · 獲贊 23 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章