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

题目大意 
填掉小的湖面  使数量精确等于k

水到极致了 
但是比赛的时候把i写成0
上限写死    脑子犯浑。。

这个题明显dfs的话代码量会小一点,出于对并查集的熟悉还是先写了并查。。 代码炒鸡长而且丑  不过跑得飞快
啊啊啊啊啊,还是太菜了啊啊啊啊啊

#include <stdio.h>
#include <iostream>
#include <stack>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
#include<cctype>
using namespace std;
typedef long long ll;
int n,m,k;
char ch[][];
int val[][];
int par[];
int cnt[];
bool vis[];
void init()
{
memset(ch,'.',sizeof(ch));
//memset(vis,false,sizeof(vis));
int tot = ;
for(int i=;i<=n+;i++)
{
for(int j=;j<=m+;j++)
{
val[i][j] = ++tot;
}
}
for(int i=;i<=tot;i++)
{
par[i] = i;
cnt[i] = ;
}
}
int find(int x)
{
if(x==par[x]) return x;
return par[x] = find(par[x]);
}
void unite(int x,int y)
{
x = find(x);
y = find(y);
if(x==y) return ;
else{
par[x] = y;
cnt[y] += cnt[x];
}
}
void make(int i,int j)
{
int dx[] = {,,,-};
int dy[] = {,-,,};
for(int k=;k<;k++)
{
if(ch[dx[k]+i][dy[k]+j]=='.')
unite(val[i][j],val[dx[k]+i][dy[k]+j]);
}
}
vector<int>v;
bool cmp(int x,int y)
{
return cnt[x]<cnt[y];
}
int main()
{
scanf("%d%d%d",&n,&m,&k);
init();
for(int i=;i<=n+;i++)
{
unite(val[][],val[i][]);
unite(val[][],val[i][m+]);
}
for(int i=;i<=m+;i++)
{
unite(val[][],val[][i]);
unite(val[][],val[n+][i]);
}
for(int i=;i<=n;i++)
{
scanf("%s",ch[i]+);
ch[i][m+] = '.';
}
for(int i=;i<=n;++i)
for(int j=;j<=m;j++)
{
if(ch[i][j]=='.') make(i,j);
}
int tt = ;
for(int i=;i<=n;++i)
for(int j=;j<=m;j++)
{
int rt= find(val[i][j]);
if(ch[i][j]=='.'&&find(val[][])!=rt)
{
if(vis[rt]) continue;
else vis[rt] = true;
v.push_back(rt);
tt++;
}
}
sort(v.begin(),v.end(),cmp);
int len = v.size();
int ans = ;
int st = ;
while(tt>k)
{
for(int i=st;i<len;i++)
{
int rt = find(v[i]);
if(vis[rt])
{
tt--;
vis[rt] = false;
for(int i=;i<=n;++i)
for(int j=;j<=m;j++)
if(find(val[i][j]) == rt) {ch[i][j] = '*';ans++;}
break;
}
}
st++;
}
cout<<ans<<endl;
for(int i=;i<=n;++i)
{
for(int j=;j<=m;j++)
{
putchar(ch[i][j]);
}
putchar('\n');
} return ;
}

AC代码

Codeforces Round #375 (Div. 2) D. Lakes in Berland (DFS或并查集)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

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

    http://codeforces.com/contest/723/problem/D 这题是只能把小河填了,题目那里有写,其实如果读懂题这题是挺简单的,预处理出每一块的大小,排好序,从小到大填就行了 ...

  5. Codeforces Round #286 (Div. 1) D. Mr. Kitayuta's Colorful Graph 并查集

    D. Mr. Kitayuta's Colorful Graph Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/ ...

  6. Codeforces Round #375 (Div. 2)

    A. The New Year: Meeting Friends 水 #include <set> #include <map> #include <stack> ...

  7. Codeforces Round #375 (Div. 2) ABCDE

    A - The New Year: Meeting Friends 水 #include<iostream> #include<algorithm> using namespa ...

  8. Codeforces Round #375 (Div. 2) - D

    题目链接:http://codeforces.com/contest/723/problem/D 题意:给定n*m小大的字符矩阵.'*'表示陆地,'.'表示水域.然后湖的定义是:如果水域完全被陆地包围 ...

  9. Codeforces Round #375 (Div. 2) - C

    题目链接:http://codeforces.com/contest/723/problem/C 题意:给定长度为n的一个序列.还有一个m.现在可以改变序列的一些数.使得序列里面数字[1,m]出现次数 ...

随机推荐

  1. 转: centos系统home下的中文目录改为英文目录

    转自h t t p : / /xugang-1017-126-com.iteye.com/blog/2081845 如果安装了中文版的Cent OS之后,root目录和home目录下会出现中文的路径名 ...

  2. Android so 库按需打包

    Fresco 大部分的代码是由Java写的,但是里面也有很多C++的代码.C++代码必须根据Android 设备的CPU类型(通常称为”ABIs”)进行编译.目前Fresco支持五种 ABI: arm ...

  3. PIE SDK矢量数据的投影转换

    1. 功能简介 目前在地理信息领域中数据包括矢量和栅格两种数据组织形式 ,每一种数据都可以对投影进行转换,目前PIE SDK支持矢量和栅格数据的投影转换功能,下面对矢量数据的投影转换功能进行介绍. 2 ...

  4. Farey Sequence(欧拉函数板子题)

    题目链接:http://poj.org/problem?id=2478 Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  5. xshell SSH 连接出现 outgoing encryption ,或者no matching host key algorithm found错误的解决

    首先看看xshell的使用版本,如果是xshell 4,提示的信息为:no matching host key algorithm found 如果是xshell 5,提示的是: outgoing e ...

  6. 【ExtJS】关于constructor、initComponent、beforeRender

    ExtJS提供的组件非常丰富,不过当原生的组件无法满足要求时,就需要扩展原生自定义组件了. initComponent 和 constructor 就是Extjs 提供用来实现继承和扩展的方式. 在E ...

  7. javaweb九大个内置对象,四大域

    9个内置对象如下: 1.session对象:会话对象 当客户端第一次访问服务器的页面时,web服务器会自动为该客户端创建一个session对象并分配一个唯一的id号 常常用它来在多个页面间共享数据,如 ...

  8. 弹性布局学习-详解 align-items(四)

    目录 弹性布局学习-介绍(一)  弹性布局学习-详解 flex-direction[决定主轴的方向](二) 弹性布局学习-详解 justify-content(三) 弹性布局学习-详解 align-i ...

  9. 05.while循环的练习

    练习1: namespace _05.while循环练习01 { class Program { static void Main(string[] args) { //打印100次"努力学 ...

  10. 【HTML&CSS】文本的基本处理

    其实在写这篇博客的时候已经学了很久,也写了不少代码,特别是很枯燥的看完整个html部分,因为不带有CSS写出来的东西干巴巴的一点也不好看. 直到展开CSS学习才开来补上博客,嗯,这是个好习惯. 这是运 ...