D. Lakes in Berland(dfs好题)

5 篇文章 0 订阅



点击打开题目


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.


题意:“*”代表土地,“.”代表水,被土地包围的是湖泊,问在现有基础上只留有k个湖泊,则至少需要填掉多少湖泊的面积(假设每个“.”面积为1);

注:在边界上的水不被陆地包围,所以在边界上的,或者与边界相连的水不是湖泊。

题解:先利用dfs搜索有哪几块湖泊,并记录湖泊的位置的面积,利用sort对湖泊面积从小到大排序,再利用dfs和所记录的湖泊位置进行湖泊填补。

dfs搜索时加上边界判断,如果是边界的话,标记flag=1;不能return,否则会搜索不完全;注:利用dfs填补的时候要进行vis初始化。


代码:


#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#define N 55
using namespace std;

int n,m,k,vis[N][N],ans,flag;
int fx[4]={0,0,1,-1},fy[4]={1,-1,0,0};
char mapp[N][N];
struct node{//利用结构体记录湖泊位置和面积
	int step;
	int xi,yi;
}num[N*N];

bool cmp(node a,node b){//按面积排序
	return a.step<b.step;
}

void dfs(int x,int y){//搜索湖泊函数
	vis[x][y]=1;
	if((x==0||x==n-1||y==0||y==m-1)&&mapp[x][y]=='.')
			flag=1;//不能return,否则搜索不完全。
	for(int i=0;i<4;i++){
		int nx=x+fx[i];
		int ny=y+fy[i];
		if((nx==0||nx==n-1||ny==0||ny==m-1)&&mapp[nx][ny]=='.')
			flag=1;
		if(nx>=0 && nx<n && ny>=0 && ny<m && !vis[nx][ny] && mapp[nx][ny]=='.'){
			num[ans].step++;
			dfs(nx,ny);
		}
	}
}

void formMap(int x,int y){//填补湖泊函数
	vis[x][y]=1;
	mapp[x][y]='*';
	for(int i=0;i<4;i++){
		int nx=x+fx[i];
		int ny=y+fy[i];
		if(nx>=0 && nx<n && ny>=0 && ny<m && !vis[nx][ny] && mapp[nx][ny]=='.'){
			formMap(nx,ny);
		}
	}
}

int main(){
	while(cin>>n>>m>>k){
		getchar(); 
		ans=0;
		flag=0;
		memset(vis,0,sizeof(vis));
		for(int i=0;i<n;i++)
		cin>>mapp[i];
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				if(mapp[i][j]=='.' && !vis[i][j]){
					ans++;
					num[ans].step=1;
					num[ans].xi=i;
					num[ans].yi=j;
					dfs(i,j);
				}
				if(flag){
					ans--;//如果该湖泊与边界相连,则减去。
					flag=0;
				}
			}
		}
		int temp=0;
		sort(num+1,num+ans+1,cmp);//vis初始化
		memset(vis,0,sizeof(vis));
		for(int i=1;i<=ans-k;i++){
			temp+=num[i].step;
			formMap(num[i].xi,num[i].yi);
		}
		cout<<temp<<endl;
		for(int i=0;i<n;i++)
		cout<<mapp[i]<<endl;
	}
	return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值