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其实是一个<form> <form id="loginForm"> <table ali ...

  2. 4816 江哥的dp题b

    4816 江哥的dp题b  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 给出两个1-N的随机排列A,B.若 ...

  3. Protocol https not supported or disabled in libcurl

    最后用PHP Curl 模拟访问HTTPS ,总是得到 Protocol https not supported or disabled in libcurl 错误,奇怪了,找了很多资料,有人说没有开 ...

  4. ASP.NET MVC 教程-MVC简介

    ASP.NET 是一个使用 HTML.CSS.JavaScript 和服务器脚本创建网页和网站的开发框架. ASP.NET 支持三种不同的开发模式:Web Pages(Web 页面).MVC(Mode ...

  5. Linux下who命令之C语言实现

    Linux下who命令之C语言实现 Step1:前期准备 首先要有一个清楚的认识:linux中一切皆文件 实现who命令,who命令也是Linux中的一个文件,那我们怎么找到它呢?我们可以" ...

  6. 我在 CSDN 的小窝

    以后有文章,我会同时更新 博客园 和 CSDN. CSDN:http://blog.csdn.net/u010918003

  7. windows 下 redis for php 配置

    下载 redis,下载地址 https://github.com/dmajkic/redis/downloads,下载下来 zip 文件,解压,根据系统选择解压的文件夹(比如我的是 64bit). 我 ...

  8. html文本标准模式,首行空两格,两端对齐,行高

    font-size: 13px; line-height: 1.6; text-align: justify; text-indent: 2em;

  9. 特殊约束From To

    说实话这个不太懂,没用过也没有遇到相应的情况(或者说我不知道).大家可以更多的去参考特定约束FROM TO和MicroZed开发板笔记,第72部分:多周期约束等内容. 本文待修正 系列目录      ...

  10. 化茧成蝶,开源NetWorkSocket通讯组件

    前言 前后历时三年,期间大量参考.Net Framework和Asp.net MVC源代码,写写删删再重构,组件如今更新到V1.5.x了.从原来的丑小鸭,变成今天拥有稳定和强大的tcp协议支持基础层, ...