Codeforces723 D. Lakes in Berland (BFS)

题目连接:http://codeforces.com/contest/723/problem/D


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




题目大意:n*m的图里,‘.’为湖,‘*’为陆地,'.'在边界的是与大海相连(即不为湖),上下左右相连的为同一个湖或者同一片海,现在要求只留下k个湖,问要最少填多少个格子。


解题思路:把所有的湖找出来,按照湖的大小排序,把小的湖填掉,只留下k个湖。


我这里两次bfs做。第一次先把湖找出来,并记录湖的大小,然后存在结构体node中,最后sort一下,把要填的湖再bfs一下把这个湖变成陆地。


很水的BFS……


/* ***********************************************
┆  ┏┓   ┏┓ ┆
┆┏┛┻━━━┛┻┓ ┆
┆┃       ┃ ┆
┆┃   ━   ┃ ┆
┆┃ ┳┛ ┗┳ ┃ ┆
┆┃       ┃ ┆
┆┃   ┻   ┃ ┆
┆┗━┓ 马 ┏━┛ ┆
┆  ┃ 勒 ┃  ┆      
┆  ┃ 戈 ┗━━━┓ ┆
┆  ┃ 壁     ┣┓┆
┆  ┃ 的草泥马  ┏┛┆
┆  ┗┓┓┏━┳┓┏┛ ┆
┆   ┃┫┫ ┃┫┫ ┆
┆   ┗┻┛ ┗┻┛ ┆
************************************************ */

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <bitset>
using namespace std;

#define rep(i,a,b) for (int i=(a),_ed=(b);i<=_ed;i++)
#define per(i,a,b) for (int i=(b),_ed=(a);i>=_ed;i--)
#define pb push_back
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define mod 1000000007
#define LL long long
#define ULL unsigned long long
#define MS0(X) memset((X), 0, sizeof((X)))
#define SelfType int
SelfType Gcd(SelfType p,SelfType q){return q==0?p:Gcd(q,p%q);}
SelfType Pow(SelfType p,SelfType q){SelfType ans=1;while(q){if(q&1)ans=ans*p;p=p*p;q>>=1;}return ans;}
#define Sd(X) int (X); scanf("%d", &X)
#define Sdd(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define Sddd(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define reunique(v) v.resize(std::unique(v.begin(), v.end()) - v.begin())
#define all(a) a.begin(), a.end()
#define   mem(x,v)      memset(x,v,sizeof(x))
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef vector<int> vi;
typedef vector<long long> vll;
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,rx=getchar();return ra*fh;}
//#pragma comment(linker, "/STACK:102400000,102400000")

char mp[55][55];
int vis[55][55];
int vis2[55][55];
int xx[4] = {1,0,-1,0};
int yy[4] = {0,1,0,-1};
int n,m,k;

struct node
{
    int x,y;
};

struct So
{
    int idx,idy,num;
}so[3000];

bool cmp(So a,So b)
{
    return a.num<b.num;
}

bool check(int x,int y)
{
    if(x<=n && x>0 && y<=m && y>0 && mp[x][y]=='.')return true;
    return false;
}

int bfs(int x,int y)
{
    queue<node>q;
    node temp;
    temp.x = x;
    temp.y = y;
    vis[x][y] = 1;
    q.push(temp);
    int flag = 1;
    if(x==n || x==1 || y==m || y==1)flag = 0;
    int cnt = 1;
    while(!q.empty())
    {
        temp = q.front();
        q.pop();
        for(int i=0;i<4;i++)
        {
            int tx = temp.x + xx[i];
            int ty = temp.y + yy[i];
            if(check(tx,ty) && !vis[tx][ty])
            {
                vis[tx][ty] = 1;
                if(tx==n || tx==1 || ty==m || ty==1)flag = 0;
                q.push(node{tx,ty});
                cnt++;
            }
        }

    }
    if(flag==0) return 0;
    return cnt;
}

int ans;

void bfs2(int x,int y)
{
    queue<node>q;
    node temp;
    temp.x = x;
    temp.y = y;
    vis2[x][y] = 1;
    q.push(temp);
    mp[x][y] = '*';
    ans++;
    //int cnt = 1;
    while(!q.empty())
    {
        temp = q.front();
        q.pop();
        for(int i=0;i<4;i++)
        {
            int tx = temp.x + xx[i];
            int ty = temp.y + yy[i];
            if(check(tx,ty) && !vis2[tx][ty])
            {
                vis2[tx][ty] = 1;
                q.push(node{tx,ty});
                mp[tx][ty] = '*';
                ans++;
            }
        }

    }
    //return cnt;
}


int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	n = read(), m = read(), k = read();
	MS0(vis);
	MS0(vis2);
	for(int i=1;i<=n;i++)
        scanf("%s",mp[i]+1);
    int tot = 0;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if(mp[i][j]=='.' && !vis[i][j])
            {
                int kk = bfs(i,j);
                if(kk)so[tot++] = So{i,j,kk};
            }
        }
    }
	sort(so,so+tot,cmp);
	//printf("%d\n",tot);
	ans = 0;
    tot = tot - k;
	for(int i=0;i<tot;i++)
    {
        int x = so[i].idx, y = so[i].idy;
        bfs2(x,y);
    }
	printf("%d\n",ans);
	for(int i=1;i<=n;i++)
        printf("%s\n",mp[i]+1);
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值