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.

```#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;
][];
][];
];
];
];
void init()
{
memset(ch,'.',sizeof(ch));
//memset(vis,false,sizeof(vis));
;
;i<=n+;i++)
{
;j<=m+;j++)
{
val[i][j] = ++tot;
}
}
;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)
{
,,,-};
,-,,};
;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();
;i<=n+;i++)
{
unite(val[][],val[i][]);
unite(val[][],val[i][m+]);
}
;i<=m+;i++)
{
unite(val[][],val[][i]);
unite(val[][],val[n+][i]);
}
;i<=n;i++)
{
scanf();
ch[i][m+] = '.';
}
;i<=n;++i)
;j<=m;j++)
{
if(ch[i][j]=='.') make(i,j);
}
;
;i<=n;++i)
;j<=m;j++)
{
int rt= find(val[i][j]);
][])!=rt)
{
if(vis[rt]) continue;
else vis[rt] = true;
v.push_back(rt);
tt++;
}
}
sort(v.begin(),v.end(),cmp);
int len = v.size();
;
;
while(tt>k)
{
for(int i=st;i<len;i++)
{
int rt = find(v[i]);
if(vis[rt])
{
tt--;
vis[rt] = false;
;i<=n;++i)
;j<=m;j++)
if(find(val[i][j]) == rt) {ch[i][j] = '*';ans++;}
break;
}
}
st++;
}
cout<<ans<<endl;
;i<=n;++i)
{
;j<=m;j++)
{
putchar(ch[i][j]);
}
putchar('\n');
}

;
}```

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&#39;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. *** \$CI =&amp; get_instance() 用法：关于CodeIgniter中get_instance() 函数

使用场景: 注意 get_instance 的使用场景,这个方法并不是用在控制器中的.而是用在控制器外面,比如类库中,想操作 CI 超级对象的时候,超级对象实际上就是当前控制器的实例. 你随便下个CI ...

2. Android 笔记 AutoCompleteTextView day8

用于自动补全内容 适应器可用于显示多行内容 package com.supermario.autocompletedemo; import android.app.Activity; import a ...

3. 同步内核缓冲区 sync、fsync和fdatasync函数

同步内核缓冲区 1.缓冲区简单介绍 人生三大错觉之中的一个:在调用函数write()时,我们觉得该函数一旦返回,数据便已经写到了文件里.可是这样的概念仅仅是宏观上的.实际上.操作系统实现某些文件I/O ...

4. wsdl生成的客户端

首先, 你要先把你的WS服务启动起来,就是 比如ht tp:/ /localhost:8080/Example/services/HelloWorldService?wsdl然后在你的另一个项目中建一 ...

5. 「设计模式」JavaScript - 设计模式之单例模式与场景实践

单例介绍 上次总结了设计模式中的module模式,可能没有真真正正的使用在场景中,发现效果并不好,想要使用起来却不那么得心应手, 所以这次我打算换一种方式~~从简单的场景中来看单例模式, 因为Java ...

6. python笔记十（列表生成式、字典生成式、生成器、生成器的并行）

一.列表生成式 列表生成式就是python设置的可以用来可以生成列表的. 如要生成一个0-9的列表我们可以通过以下代码实现: >>> list(range(10)) [0, 1, 2 ...

7. Java基础----Java---集合框架---泛型、泛型方法、静态方法泛型、泛型接口、泛型限定、泛型类

泛型:jdk1.5后的新特性,用于解决安全问题,是一个安全机制. 好处: 1.将运行时的异常出现问题classcastException.转移到了编译时期.方便程序员调试解决问题,让运行事情问题减少, ...

8. 【转载】C#常用数据库Sqlserver通过SQL语句查询数据库以及表的大小

在Sqlserver数据库中,一般我们查看数据库的大小可以通过查找到数据库文件来查看,但如果要查找数据表Table的大小的话,则不可通过此方法,在Sqlserver数据库中,提供了相应的SQL语句来查 ...

9. day038 navicat pymysql

今日内容: 1.navicat 2.pymysql 1.navicat 需要掌握 #1. 测试+链接数据库 #2. 新建库 #3. 新建表,新增字段+类型+约束 #4. 设计表:外键 #5. 新建查询 ...

10. 【MOOC EXP】Linux内核分析实验二报告

程涵  原创博客 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000  [操作系统是如何工作的]   教学内 ...