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. web standards

    http://www.w3.org/standards/ http://www.webstandards.org/learn/faq/#p213 http://www.w3.org/standards ...

  2. grains

    用途 1,匹配客户端 2,配置文件里使用 3,资产管理     定义grains方法1:             方法2:        

  3. 数据库mark

    LOAD DATA INFILE 'I:\QQpwd\\1.txt' IGNORE INTO TABLE sgk.top1 FIELDS TERMINATED BY '----' OPTIONALLY ...

  4. TRIGGER command denied to user 'root'@'LAPTOP-M7KUFN86' for table 'growtest' | Table 'MyDatabase.tmpIdentity_Invites' doesn't exist

    是因为创建表的时候,用户权限不够 NaviCat for Mysql 用这个工具打开MYSQL 在用户 下找到 root@% 这个用户,双击打开 设置服务器权限,最后两个权限勾上就OK 了,需要把MY ...

  5. Qt Creator 常用快捷键

    多行注释模式                                                                                            Ct ...

  6. 4.5你太黑了,不带这么玩TypeForwardedTo的

    话说最近好不容易把framework 4.0的metadata信息都能全部抽出了,结果换4.5挂了...framework那帮人在4.5里面用了些什么诡异的玩意? 结果一看4.5的部分field用了t ...

  7. HTML5之CSS3 3D transform 剖析式学习之一

    最近坐地铁发现“亚洲动物基金”在地铁上做了很多公益广告,比较吸引人的是一个月熊的广告.做的很可爱.回去就搜了一下,发现这个网站是HTML5做的,非常炫. 所以想学习一下,方法就是传统的学习办法,模仿. ...

  8. PowerShell Script to Deploy Multiple VM on Azure in Parallel #azure #powershell

    Since I need to deploy, start, stop and remove many virtual machines created from a common image I c ...

  9. DNS资源纪录(Resource Record)介绍

          http://dns-learning.twnic.net.tw/bind/intro6.html 类型 SOA NS A AAAA PTR CNAME MX -------------- ...

  10. jq不包含某属性

    jq解释属性选择器时有以下四种: 上面都是带某属性或者属性为某值的情况,还有一种情况是不带某属性怎么办? 答案是同属性不为某值. 如 <a b='c' class="d"&g ...