codeforce 723d Lakes in Berland(暴力DFS)


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.



题意:给出一个地图:.是水,*是陆地,地图外边是海。这样里边就有湖泊,淡然与海相连的水就不是湖泊。问我要填掉一些湖泊,使得剩下的湖泊个数恰好为k,问最少要填多少水。



解题思路: 首先处理一下哪些是海,先从边界开始DFS。接着开始暴力每个点DFS,记录湖边的个数,和所需填多少水。  最好记录下来排个序,再DFS,填上


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
char a[111][111];
int vis[111][111];
int n,m,cc;
struct node
{
	int x,y,number;
}b[11111];
bool cmp(node t1,node t2)
{
	if(t1.number<t2.number) return true;
	return false;
}
int dx[]={-1,0,1,0},dy[]={0,1,0,-1};

void dfs1(int x,int y)
{
	int i,j;
	for(i=0;i<4;i++) {
		int tx=x+dx[i];
		int ty=y+dy[i];
		if(tx<0||tx>n-1||ty<0||ty>m-1) continue;
		if(vis[tx][ty]==0&&a[tx][ty]=='.') {
			vis[tx][ty]=1;
			dfs1(tx,ty);
		}
	}
}

int dfs2(int x,int y)
{
	int i,j;
	for(i=0;i<4;i++) {
		int tx=x+dx[i];
		int ty=y+dy[i];
		if(tx<0||tx>n-1||ty<0||ty>m-1) continue;
		if(vis[tx][ty]==0&&a[tx][ty]=='.') {
			cc++;
			vis[tx][ty]=1;
			dfs2(tx,ty);
		}
	}
}

void dfs3(int x,int y)
{
	int i,j;
	for(i=0;i<4;i++) {
		int tx=x+dx[i];
		int ty=y+dy[i];
		if(tx<0||tx>n-1||ty<0||ty>m-1) continue;
		if(a[tx][ty]=='.') {
			a[tx][ty]='*';
			dfs3(tx,ty);
		}
	}
}

int main()
{
	int num,i,j,p;
	scanf("%d%d%d",&n,&m,&num);
	for(i=0;i<n;i++) scanf("%s",a[i]);
	//处理边界的湖 
	///
	//
	for(i=0;i<n;i++) {
		for(j=0;j<m;j++) {
			if(i==0||j==0||i==n-1||j==m-1) {
				if(a[i][j]=='.'&&vis[i][j]==0) {
					vis[i][j]=1;
		    		dfs1(i,j);
				}
			}
		}
	}
	
	
	
	//处理湖 
	p=0;
	for(i=0;i<n;i++) {
		for(j=0;j<m;j++) {
			if(vis[i][j]==0&&a[i][j]=='.') {
				cc=1;
				vis[i][j]=1;
				dfs2(i,j);
				b[++p].number=cc;
				b[p].x=i;
				b[p].y=j;
			}
		}
	}
	int ans=0;
	sort(b+1,b+1+p,cmp);
	for(i=1;i<=p-num;i++) {
		ans+=b[i].number;
		a[b[i].x][b[i].y]='*';
		dfs3(b[i].x,b[i].y);
	}
	printf("%d\n",ans);
	for(i=0;i<n;i++) printf("%s\n",a[i]);
	return 0;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值