D. Lakes in Berland
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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代表期望的湖的数量。
对地图进行更改操作,使得湖的数量变为K,同时保证修改的数量最小。
输出最小修改数量和修改后的图。
思路:首先对于图的边缘部分是水的情况进行预处理标记,和这些水相连的部分称为海。
接着统计湖的数量,如果太多就按照面积从小到大更改图,如果太少,就遍历图仅更改一个满足条件的陆地即可。
我是用广搜处理的,深搜当然也可以
代码
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
#include<queue>
using namespace std;
const int maxn=55;
char map[maxn][maxn];
bool vis[maxn][maxn];
int dis[4][2]= {{0,1},{0,-1},{1,0},{-1,0}};
struct node
{
int x;
int y;
};
vector<node>VC[maxn*maxn];//第i个湖的坐标集合是VC[i]
struct edge
{
int id;//第id个湖
int num;//包含num个坐标
};
edge num[maxn*maxn];
int num_lakes=0;//湖的数量
int N,M,K;
bool cmp(edge x,edge y)
{
return x.num<y.num;
}
bool judge(int x,int y)
{
if(x<0||x>=N||y<0||y>=M||map[x][y]!='.'||vis[x][y]!=0)
return false;
return true;
}
void BFS(int x,int y)
{
node star;
star.x=x;
star.y=y;
vis[x][y]=1;
queue<node>q;
q.push(star);
while(!q.empty())
{
star=q.front();
q.pop();
for(int i=0; i<4; i++)
{
node endn=star;
endn.x+=dis[i][0];
endn.y+=dis[i][1];
if(judge(endn.x,endn.y)==true)
{
vis[endn.x][endn.y]=1;
q.push(endn);
}
}
}
}
bool judge_VC(int x,int y)
{
if(x<=0||x>=N||y<=0||y>=M||vis[x][y]==1||map[x][y]!='.')
return false;
return true;
}
void BFS_VC(int x,int y)
{
node star;
star.x=x;
star.y=y;
VC[num_lakes].push_back(star);
vis[x][y]=1;
queue<node>q;
q.push(star);
while(!q.empty())
{
star=q.front();
q.pop();
for(int i=0; i<4; i++)
{
node endn=star;
endn.x+=dis[i][0];
endn.y+=dis[i][1];
if(judge(endn.x,endn.y)==true)
{
VC[num_lakes].push_back(endn);
vis[endn.x][endn.y]=1;
q.push(endn);
}
}
}
}
int main()
{
scanf("%d%d%d",&N,&M,&K);
for(int i=0; i<N; i++)
scanf("%s",map[i]);
memset(vis,0,sizeof(vis));
//处理边缘
for(int j=0; j<M; j++)
{
if(map[0][j]=='.'&&vis[0][j]==0)
BFS(0,j);
if(map[N-1][j]=='.'&&vis[N-1][j]==0)
BFS(N-1,j);
}
for(int i=0; i<N; i++)
{
if(map[i][0]=='.'&&vis[i][0]==0)
BFS(i,0);
if(map[i][M-1]=='.'&&vis[i][M-1]==0)
BFS(i,M-1);
}
//处理核心
for(int i=1; i<N-1; i++)
{
for(int j=1; j<M-1; j++)
{
if(map[i][j]=='.'&&vis[i][j]==0)
{
BFS_VC(i,j);
num_lakes++;
}
}
}
int num_change=0;//改变的单元格数量
if(num_lakes>K)//现有湖泊数量过多
{
for(int i=0; i<num_lakes; i++)
{
num[i].id=i;
num[i].num=(int)VC[i].size();
}
sort(num,num+num_lakes,cmp);
for(int i=0; i<num_lakes-K; i++)
{
num_change+=num[i].num;//第i小岛屿的坐标集合数量
for(int j=0; j<(int)VC[num[i].id].size(); j++)
{
node point=VC[num[i].id][j];
map[point.x][point.y]='*';
}
}
}
else if(num_lakes<K)
{
for(int i=1; i<N-1&&num_lakes<K; i++)
{
for(int j=1; j<M-1&&num_lakes<K; j++)
{
if(map[i][j]=='*'&&map[i-1][j]=='*'&&map[i+1][j]=='*'&&map[i][j-1]=='*'&&map[i][j+1]=='*')
{
map[i][j]='*';
num_change++;
num_lakes++;
}
}
}
}
printf("%d\n",num_change);
for(int i=0; i<N; i++)
{
for(int j=0; j<M; j++)
printf("%c",map[i][j]);
printf("\n");
}
return 0;
}