Description

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.

正解:BFS

解题报告:

  根据题目意思,先把海洋部分找出来,标记一下(BFS即可)。然后对于所有的处在内部的湖,BFS求出每个湖的面积,并且按湖的面积排序,优先把面积小的湖填掉。

  结果一直RE+WA的原因居然是BFS的时候,我的标记经过的数组是一个萎的,我是在从队首取出时才标记的,事实上入队的时候就应该考虑!注意细节!

 //It is made by jump~
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
typedef long long LL;
const int inf = (<<);
const int MAXN = ;
const int MAXL = ;
const int MOD = ;
int n,m,k,cnt,ans;
int a[MAXN][MAXN];
bool vis[MAXN][MAXN];
int head,tail,dui[MAXL][];
int yi[][]={{,},{-,},{,},{,-}};
struct lake{
int val;
int x,y;
}hu[MAXN*MAXN];
inline bool cmp(lake q,lake qq){ return q.val<qq.val; }
inline int getint()
{
int w=,q=; char c=getchar();
while((c<'' || c>'') && c!='-') c=getchar(); if(c=='-') q=,c=getchar();
while (c>='' && c<='') w=w*+c-'', c=getchar(); return q ? -w : w;
} inline void get_ocean(int inx,int iny){
head=tail=; dui[++tail][]=inx; dui[tail][]=iny; vis[inx][iny]=;
int ux,uy,nowx,nowy;
while(head!=tail) {
head++; head%=MOD; ux=dui[head][]; uy=dui[head][];
for(int i=;i<;i++) {
nowx=ux+yi[i][]; nowy=uy+yi[i][]; if(nowx<= || nowy<= || nowx>n || nowy>m) continue;
if(!a[nowx][nowy]) continue; if(vis[nowx][nowy]) continue;
tail++; tail%=MOD; dui[tail][]=nowx; dui[tail][]=nowy; vis[nowx][nowy]=;
}
}
} inline int BFS(int inx,int iny){
head=tail=; dui[++tail][]=inx; dui[tail][]=iny; vis[inx][iny]=;
int ux,uy,nowx,nowy,total=;
while(head!=tail) {
head++; head%=MOD; ux=dui[head][]; uy=dui[head][];
total++;
for(int i=;i<;i++) {
nowx=ux+yi[i][]; nowy=uy+yi[i][]; if(nowx<= || nowy<= || nowx>n || nowy>m) continue;
if(!a[nowx][nowy]) continue; if(vis[nowx][nowy]) continue;
tail++; tail%=MOD; dui[tail][]=nowx; dui[tail][]=nowy; vis[nowx][nowy]=;
}
}
return total;
} inline void change(int inx,int iny){
head=tail=; dui[++tail][]=inx; dui[tail][]=iny; vis[inx][iny]=;
int ux,uy,nowx,nowy;
while(head!=tail) {
head++; head%=MOD; ux=dui[head][]; uy=dui[head][]; a[ux][uy]=;
for(int i=;i<;i++) {
nowx=ux+yi[i][]; nowy=uy+yi[i][]; if(nowx<= || nowy<= || nowx>n || nowy>m) continue;
if(!a[nowx][nowy]) continue; if(vis[nowx][nowy]) continue;
tail++; tail%=MOD; dui[tail][]=nowx; dui[tail][]=nowy; vis[nowx][nowy]=;
}
}
} inline void work(){
n=getint(); m=getint(); k=getint(); char c;
for(int i=;i<=n;i++) for(int j=;j<=m;j++) { c=getchar(); while(c!='*' && c!='.') c=getchar(); if(c=='.') a[i][j]=; }
for(int i=;i<=n;i++) { if(!vis[i][] && a[i][]) get_ocean(i,); if(!vis[i][m] && a[i][m]) get_ocean(i,m); }
for(int i=;i<m;i++) { if(!vis[][i] && a[][i]) get_ocean(,i); if(!vis[n][i] && a[n][i]) get_ocean(n,i); }
for(int i=;i<n;i++) for(int j=;j<m;j++) if(!vis[i][j] && a[i][j]) { hu[++cnt].val=BFS(i,j); hu[cnt].x=i; hu[cnt].y=j; }
memset(vis,,sizeof(vis)); sort(hu+,hu+cnt+,cmp);
for(int i=;i<=cnt-k;i++) { ans+=hu[i].val; change(hu[i].x,hu[i].y); }
printf("%d\n",ans);
for(int i=;i<=n;i++) { for(int j=;j<=m;j++) if(a[i][j]) printf("."); else printf("*"); printf("\n"); }
} int main()
{
work();
return ;
}

codeforces 723D: Lakes in Berland的更多相关文章

  1. 【29.70%】【codeforces 723D】Lakes in Berland

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  2. 【Codeforces 723D】Lakes in Berland (dfs)

    海洋包围的小岛,岛内的有湖,'.'代表水,'*'代表陆地,给出的n*m的地图里至少有k个湖,求填掉面积尽量少的水,使得湖的数量正好为k. dfs找出所有水联通块,判断一下是否是湖(海水区非湖).将湖按 ...

  3. Codeforces Round #375 (Div. 2) D. Lakes in Berland 贪心

    D. Lakes in Berland 题目连接: http://codeforces.com/contest/723/problem/D Description The map of Berland ...

  4. Codeforces Round #375 (Div. 2)——D. Lakes in Berland(DFS连通块)

    D. Lakes in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  5. Codeforces Round #375 (Div. 2) D. Lakes in Berland dfs

    D. Lakes in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  6. Codeforces Round #375 (Div. 2) D. Lakes in Berland (DFS或并查集)

    D. Lakes in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  7. CF723D. Lakes in Berland[DFS floodfill]

    D. Lakes in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  8. codeforces723 D. Lakes in Berland(并查集)

    题目链接:codeforces723 D. Lakes in Berland 参考博客:http://www.cnblogs.com/Geek-xiyang/p/5930245.html #inclu ...

  9. codeforce375div2-D. Lakes in Berland 搜索

    Lakes in Berland 题意与解释:这道题就是求图中被围起来的点群,问最少去掉几个点,可以使得孤立的点群数目为K; 因为自己写的代码又长又had bugs. 我自己写的bfs,想着是先染色, ...

随机推荐

  1. 窗口 - dialog - 概述与基本使用

    什么是dialog 对话框是一种特殊的窗口,它在顶部有一个工具栏,在底部有一个按钮栏.默认情况下,对话框(dialog)只有一个显示在头部右侧的关闭工具. 用户可以配置对话框行为来显示其他工具(比如: ...

  2. swift 定时器的使用

    在swift中,要使用定时器就需要用到对象NSTimer.通过NSTimer的实例化后,就可以调用fire方法来启用了. NSTimer有2个构造函数 init(timeInterval ti: NS ...

  3. Android — Camera聚焦流程

    原文  http://www.cnphp6.com/archives/65098 主题 Android Camera.java autoFocus()聚焦回调函数 @Override public v ...

  4. Alpha版本项目展示要求

    Alpha版本展示的时间暂定为11月17日课上.如有变动,另行通知. 在Alpha阶段项目评审会上, 每个团队有12分钟展示时间,10分钟问答和机动时间,我们的展示也不需要PPT,大家把要展现的东西写 ...

  5. Python3.1-标准库之Numpy

    这系列用来介绍Python的标准库的支持Numpy部分.资料来自http://wiki.scipy.org/Tentative_NumPy_Tutorial,页面有许多链接,这里是直接翻译,所以会无法 ...

  6. bash中变量+=,if大小判断,随机休眠

    #!/bin/bash index= while true;do echo "hello" (( index+=)) echo `date "+%H:%M:%S" ...

  7. .net框架中少有人知的扩展cmod

    最近在利用metadata api抽取.net的原数据信息,发现了不少“坑”,也发现了不少常年用着c#的人都不知道的扩展. 说到.net原数据的可扩展性,第一个让人能想到的就是CustomAttrib ...

  8. 也来山寨一版Flappy Bird (js版)

    随着Flappy Bird的火爆,各种实现的版也不断出现,于是也手痒简单实现了一版. 其实本来只是想实现一下这只笨鸟的飞翔运动的,后来没忍住,就直接实现一个完整游戏了…… 因为这个游戏本身实现起来就没 ...

  9. 【bzoj1601】[Usaco2008 Oct]灌水(MST)

    题目:http://hzwer.com/1158.html 分析: 解法很巧妙,弄一个超级源,对某个点装水井相当于把这个点连向超级源,边权为这个点的点权,然后跑最小生成树就行了

  10. HTTP 错误 500.24 - Internal Server Error的解决方法

    错误提示: 最可能的原因:   system.web/identity@impersonate 设置为 true. 解决办法: 现在经典模式 连微软都几乎放弃了 原设想是为iis不断升级 提供的一种兼 ...