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. uploadify firefox 401

    uploadify在firefox下上传会报401错误:这是因为java的框架把其拦截了 拦截的原因是,firefox下的flash在请求和发送请求的时候不会携带cookie和session过去,造成 ...

  2. 重建中国.NET生态系统

    Neuzilla官方微信公众号:搜 架构师联盟 或 neuzilla 我是.NET铁杆粉丝,所以如果你要在评论里跟我撕逼.NET怎么怎么烂,Java.C++.PHP.JavaScript怎么怎么好,我 ...

  3. 架构系列:ASP.NET 项目结构搭建

    我们头开始,从简单的单项目解决方案,逐步添加业务逻辑的约束,从应用逻辑和领域逻辑两方面考虑,从简单的单个项目逐步搭建一个多项目的解决方案.主要内容:(1)搭建应用逻辑和领域逻辑都简单的单项目 (2)为 ...

  4. 171 Excel Sheet Column Number

    /** * 题意:A表示1 B表示2 AA表示27 AB表示28 ------>给你一串字符串输出相应的数字 * 分析:这个就类似于二进制转十进制,从字符串后面往前遍历,然后pow(26,n)* ...

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

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

  6. HDU5892~HDU5901 2016网络赛沈阳

    A.题意: 有一个n×n的格子, 有50种怪物. 有m个操作, 每次操作会往一个矩形区域放怪物, 每个格子放相同数目的怪物, 或者查询当前50种怪物的奇偶性. 分析:用2^50表示怪物的奇偶,然后就是 ...

  7. 51单片机中断interrupt……using……

    51单片机中断细节的一些问题. interrupt0:外部中断0interrupt1:定时器中断0interrupt2:外部中断interrupt3:定时器中断1interrupt4:串口 using ...

  8. 前端框架——BootStrap学习

    BootStrap简单总结下:1.栅格系统,能够很好的同时适应手机端和PC端(及传说中的响应式布局) 2.兼容性好 接下来是对BootStrap学习的一些基础案例总结和回顾: 首先引入:bootstr ...

  9. 访问HTML元素(节点)

    访问HTML元素等同于访问节点,能够以不同的 方式来访问HTML元素: 通过使用 getElementById() 方法 通过使用 getElementsByTagName() 方法 通过使用 get ...

  10. Hibernate的三种状态

     Hibernate的对象有3种状态,分别为:瞬时态(Transient). 持久态(Persistent).脱管态(Detached).处于持久态的对象也称为PO(Persistence Objec ...