CF723D. Lakes in Berland[DFS floodfill]

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 ≤ 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找湖

把湖从小到大填就行了

注意递归别调用错函数

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <map>
using namespace std;
typedef long long ll;
const int N=55;
inline int read(){
    char c=getchar();int x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return x*f;
}
int n,m,k;
char g[N][N];
int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1};
int vis[N][N],num[N*N],cc=0;
struct lakes{
    int size,id;
}lake[N*N];
int cnt=0;
bool cmp(lakes &a,lakes &b){
    return a.size<b.size;
}
void dfs(int x,int y,int id){//printf("dfs %d %d %d\n",x,y,id);
    vis[x][y]=id;num[id]++;
    for(int i=0;i<4;i++){
        int nx=x+dx[i],ny=y+dy[i];
        if(nx<1||nx>n||ny<1||ny>m) continue;
        if(g[nx][ny]=='*'||vis[nx][ny]) continue;
        dfs(nx,ny,id);
    }
}
int ans=0;
void fil(int x,int y,int id){//printf("fil %d %d %d\n",x,y,id);
    g[x][y]='*';ans++;
    for(int i=0;i<4;i++){
        int nx=x+dx[i],ny=y+dy[i];
        if(nx<1||nx>n||ny<1||ny>m) continue;
        if(vis[nx][ny]==id&&g[nx][ny]=='.') fil(nx,ny,id);
    }
}
int main(){
    n=read();m=read();k=read();
    for(int i=1;i<=n;i++){
        scanf("%s",g[i]);
        for(int j=m;j>=1;j--) g[i][j]=g[i][j-1];
    }
    
    for(int i=1;i<=n;i++){
        if(!vis[i][1]&&g[i][1]=='.') dfs(i,1,++cc);
        if(!vis[i][m]&&g[i][m]=='.') dfs(i,m,++cc);
    }
    for(int j=1;j<=m;j++){
        if(!vis[1][j]&&g[1][j]=='.') dfs(1,j,++cc);
        if(!vis[n][j]&&g[n][j]=='.') dfs(n,j,++cc);
    }
    int sea=cc;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++){
            if(!vis[i][j]&&g[i][j]=='.')
                dfs(i,j,++cc);
        }
    for(int i=sea+1;i<=cc;i++) {lake[++cnt].size=num[i];lake[cnt].id=i;}//printf("%d %d\n",sea,cc);
    sort(lake+1,lake+1+cnt,cmp);
    //for(int i=1;i<=cnt;i++) printf("lake %d %d\n",lake[i].id,lake[i].size);
    int t=cnt-k,p=1;//printf("t %d\n",t);
    for(int z=1;z<=t;z++){
        int fin=0;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++)
                if(lake[p].id==vis[i][j]){fil(i,j,lake[p++].id);fin=1;break;}
            if(fin) break;
        }
    }
    printf("%d\n",ans);
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++) printf("%c",g[i][j]);
        if(i!=n) putchar('\n');
    }
    
}

 

posted @ 2016-10-04 16:51  Candy?  阅读(254)  评论(0编辑  收藏  举报