codeforce#375D. Lakes in Berland

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 ≤ 500 ≤ 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.

只是简单的bfs,但是因为自己把搜索时候的标记放在了从队列里拿出来的时候。因为这一点,想了一个早上。唉,吃一堑长一智阿。

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<queue>
#include<cstring>
using namespace std;
char a[60][60];
bool vis[60][60];
int nx[] = {1, 0, -1 , 0};
int ny[] = {0, -1, 0, 1};
int n, m;
struct node
{
    int x;
    int y;
    node (int x, int y):x(x), y(y){}
};
struct NODE
{
    int x;
    int y;
    int sum;
}rain[10000];
int ans, cnt;
void bfs(int i, int j)
{
    queue<node> q;
    if(a[i][j] == '.')
    {
        q.push(node(i, j));
        while(!q.empty())
        {
            node tem = q.front();
            q.pop();
            //a[tem.x][tem.y] = '*';
            vis[tem.x][tem.y] = true;
            for(int kk = 0; kk < 4; kk++)
            {
                int tx = tem.x + nx[kk];
                int ty = tem.y + ny[kk];
                if(tx >= n || tx < 0 || ty >= m || ty < 0 || vis[tx][ty] || (a[tx][ty] == '*'))
                    continue;
                else
                {
                    q.push(node(tx, ty));
                    vis[tx][ty] = true;
                }
            }
        }
    }
}
void BFS(int x, int y)
{
    int sum = 0;
    int ansx = x, ansy = y;
    queue<node> q;
    q.push(node(x, y));
    while(!q.empty())
    {
        node tem = q.front();
        q.pop();
        sum++;
        vis[tem.x][tem.y] = true;
        for(int kk = 0; kk < 4; kk++)
        {
            int tx = tem.x + nx[kk];
            int ty = tem.y + ny[kk];
            if(tx >= n || tx < 0 || ty >= m || ty < 0 || vis[tx][ty] || (a[tx][ty] == '*'))
                continue;
            else
            {
                q.push(node(tx, ty));
                vis[tx][ty] = true;
                ansx = tx;
                ansy = ty;
            }
        }
    }
    //cout << ansx+1 << " " << ansy+1 << "...\n";
    rain[cnt].x = ansx;
    rain[cnt].y = ansy;
    rain[cnt].sum = sum;
    cnt++;
}
void bfs2(int x, int y)
{
    //cout << x+1 << " " << y+1 << endl;
    queue<node> q;
    q.push(node(x, y));
    while(!q.empty())
    {
        node tem = q.front();
        q.pop();
        a[tem.x][tem.y] = '*';
        vis[tem.x][tem.y] = true;
        for(int kk = 0; kk < 4; kk++)
        {
            int tx = tem.x + nx[kk];
            int ty = tem.y + ny[kk];
            if(tx >= n || tx < 0 || ty >= m || ty < 0 || vis[tx][ty] || (a[tx][ty] == '*'))
                continue;
            else
            {
                q.push(node(tx, ty));
                vis[tx][ty] = true;
            }
        }
    }
}
bool cmp(NODE ta, NODE tb)
{
    return ta.sum < tb.sum;
}
int main()
{
    int k;
    while(cin >>n >> m >> k)
    {
        cnt = ans = 0;
        memset(vis ,false , sizeof(vis));
        for(int i = 0; i < n; i++)
            cin >> a[i];
        for(int j = 0; j < m; j++)
        {
            vis[0][j] = true;
            vis[n-1][j] = true;
            bfs(0, j);
            bfs(n-1, j);
        }
        for(int i = 0; i < n; i++)
        {
            vis[i][0] = true;
            bfs(i, 0);
            vis[i][m-1] = true;
            bfs(i, m-1);
        }
        /*cout << endl;
        for(int i = 0; i < n; i++)
            cout << a[i] << endl;*/
        //cout << a[3][2] << endl;
        for(int i = 1; i < n; i++)
        {
            for(int j = 1; j < m; j++)
            {
                if(a[i][j] == '.' && !vis[i][j])
                {
                    BFS(i, j);
                    ans++;
                }
            }
        }
        sort(rain, rain+cnt, cmp);
        //cout << cnt << endl;
        //cout << rain[0].sum << endl;
        int sss = 0;
        memset(vis, false, sizeof(vis));
        if(cnt > k)
        {
            int tt = cnt - k;
            for(int i = 0; i < tt; i++)
            {
                sss += rain[i].sum;
                int tx = rain[i].x;
                int ty = rain[i].y;
                bfs2(tx, ty);
            }
        }
        //cout << endl;
        cout << sss << endl;
        for(int i = 0; i < n; i++)
            cout << a[i] << endl;
    }
    return 0;
}


### 如何在 Codeforces 平台上将界面语言设置为中文 Codeforces 是一个国际化的编程竞赛平台,支持多种语言的界面显示,其中包括中文。以下是关于如何将 Codeforces 的界面语言切换到中文的具体方法: #### 1. 登录账户 用户需要先登录自己的 Codeforces 账户。如果尚未注册账户,则需完成注册流程后再进行操作。 #### 2. 进入个人资料页面 点击右上角用户名旁边的下拉菜单选项,在弹出的列表中选择 **Settings**(设置)。此部分允许用户修改各种偏好设定[^1]。 #### 3. 修改语言选项 在 Settings 页面中找到 **Language** 字段,默认可能是 English 或其他语言。通过下拉框可以选择不同的语言选项,其中包含 Simplified Chinese 表示简体中文。选择后保存更改即可生效[^2]。 #### 4. 验证更改效果 返回至首页或其他任意页面确认网站文字已转换成所选目标语言——即中文环境下的表述形式。此时所有的题目描述、公告以及交互提示都将采用中文呈现给用户阅读理解[^3]。 ```python # 示例代码展示如何模拟简单的语言切换逻辑 class UserPreferences: def __init__(self, username): self.username = username self.language = 'English' def set_language(self, new_lang): supported_languages = ['English', 'Simplified Chinese'] if new_lang in supported_languages: self.language = new_lang print(f"Language switched to {new_lang} for user {self.username}.") else: print("Unsupported language.") user = UserPreferences('example_user') user.set_language('Simplified Chinese') # 切换语言为简体中文 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值