Codeforces Round #375 (Div. 2) D bfs

本文介绍了一种湖泊填充算法,用于解决地图上湖泊数量调整的问题。通过两次广度优先搜索(BFS),该算法能够有效地确定最小数量的土地填充操作,以达到预定的湖泊数量。



链接:戳这里



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.


题意:
给出n*m的图,'*'表示陆地,'.'表示湖,整个图的周围是海洋,现在要求留下k个较大湖,连接海洋的湖不算湖

思路:
第一次bfs将湖分成块
第二遍bfs将边缘的湖变成海
然后把湖的大小拿出来排序,将前k-1小的变成陆地

代码:
#include <iostream>
#include <cstdio>
#include <queue>
#include <map>
#include <algorithm>
using namespace std;
int n,m,k;
char s[110][110];
int vis[110][110];
struct node{
    int x,y;
    node(int x=0,int y=0):x(x),y(y){}
};
int xx[4]={0,1,-1,0};
int yy[4]={1,0,0,-1};
bool pd(node t){
    if(s[t.x][t.y]=='*' || vis[t.x][t.y]) return false;
    if(t.x<1 || t.x>n || t.y<1 || t.y>m) return false;
    return true;
}
bool pd1(node t,int num){
    if(t.x<1 || t.x>n || t.y<1 || t.y>m) return false;
    if(vis[t.x][t.y]==num) return true;
    return false;
}
void bfs(node p,int num){
    vis[p.x][p.y]=num;
    queue<node> qu;
    qu.push(p);
    while(!qu.empty()){
        node now=qu.front(),next;
        qu.pop();
        for(int i=0;i<4;i++){
            next.x=now.x+xx[i];
            next.y=now.y+yy[i];
            if(pd(next)){
                vis[next.x][next.y]=num;
                qu.push(next);
            }
        }
    }
}
void rebfs(node p,int num){
    vis[p.x][p.y]=1;
    queue<node> qu;
    qu.push(p);
    while(!qu.empty()){
        node now=qu.front(),next;
        qu.pop();
        for(int i=0;i<4;i++){
            next.x=now.x+xx[i];
            next.y=now.y+yy[i];
            if(pd1(next,num)){
                vis[next.x][next.y]=1;
                qu.push(next);
            }
        }
    }
}
map<int , int > mp;
struct p{
    int v,num;
    p(int v=0,int num=0):v(v),num(num){}
    bool operator < (const p &a)const{
        return num<a.num;
    }
}anw[10010];
int ans=0;
int main(){
    scanf("%d%d%d",&n,&m,&k);
    for(int i=1;i<=n;i++) {
        getchar();
        for(int j=1;j<=m;j++){
            scanf("%c",&s[i][j]);
        }
    }
    int cnt=1;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(vis[i][j]) continue;
            else if(s[i][j]=='*'){
                vis[i][j]=1;
            } else {
                bfs(node(i,j),++cnt);
            }
        }
    }
    for(int i=1;i<=m;i++){
        if(vis[1][i]!=1){
            rebfs(node(1,i),vis[1][i]);
        }
        if(vis[n][i]!=1){
            rebfs(node(n,i),vis[n][i]);
        }
    }
    for(int i=1;i<=n;i++){
        if(vis[i][1]!=1){
            rebfs(node(i,1),vis[i][1]);
        }
        if(vis[i][m]!=1){
            rebfs(node(i,m),vis[i][m]);
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(vis[i][j]==1) continue;
            mp[vis[i][j]]++;
        }
    }
    for(auto & t : mp){
        anw[++ans].v=t.first;
        anw[ans].num=t.second;
    }
    sort(anw+1,anw+ans+1);
    int ANS=0;
    for(int l=1;l<=ans-k;l++){
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                if(vis[i][j]==anw[l].v){
                    s[i][j]='*';
                    ANS++;
                }
            }
        }
    }
    printf("%d\n",ANS);
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            printf("%c",s[i][j]);
        }
        printf("\n");
    }
    return 0;
}



### 关于Codeforces Round 971 (Div. 4)比赛题目及解析 #### A-G1题解概述 对于Codeforces Round 971 (Div. 4),该轮次的比赛涵盖了多个难度级别的编程挑战,旨在测试参赛者的算法思维能力和编码技巧。其中,A至G1的题目设计覆盖了基础数据结构操作、贪心策略应用以及动态规划等多个方面。 #### G2. Yunli’s Subarray Queries (Hard Version) 具体到G2这道难题目,其背景设定在一个由n个节点构成的无向图环境中,此图恰好拥有\( n-1 \)条带权重的边,每一边连接着两个连续编号的顶点,并且初始状态下每个顶点都放置了一个与其自身编号相匹配的小球[^1]。问题的核心在于计算一种最优方案下的总成本——即通过调整各小球的位置让它们不再位于原本对应的顶点之上;每一次位置交换都需要支付相应路径上的代价(即边的权重),而目标就是找到能够满足上述条件的同时使总的迁移费用达到最低的方法。 针对此类涉及最短路径求解的问题,通常采用广度优先搜索(BFS)或迪杰斯特拉(Dijkstra&#39;s Algorithm)等经典图论算法来进行处理。然而,在本题特殊条件下,则更倾向于利用差分数组配合线段树/树状数组来高效解决区间更新与查询的需求,从而快速响应多次修改后的即时状态变化并给出正确解答[^2]。 ```cpp #include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int a[N], b[N]; long long sum[N << 2]; void push_up(int rt){ sum[rt] = min(sum[rt<<1],sum[rt<<1|1]); } // build, update and query functions omitted for brevity... int main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n,q,x,y,z; cin >> n >> q; // initialization code here... } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值