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. js css div 点亮半颗星星(二)

    上回说到js css点亮星星 换种方式来点亮 <!DOCTYPE html> <html lang="en"> <head> <meta ...

  2. c语言实现栈的增删查减

    #include"stdio.h"#include"stdlib.h"#define STACK_SIZE 100#define STACKINCREMENT ...

  3. 笔记_JSON

    解析 JSON 步骤 如果没有自带 , 就添加 第三方包 (JavaScript编程语言本身自带解析JSON的能力) 一般是要手写 : 实体类 JSON -> 实体类  中间映射 Gson的话 ...

  4. Python+Selenium之HTMLTestRunner

    下载 HTMLTestRunner 模块 下载地址:http://tungwaiyip.info/software/HTMLTestRunner.html 保存路径:将下载的HTMLTestRunne ...

  5. 20条IPTables防火墙规则用法! [转]

    20条IPTables防火墙规则用法! 导读 管理网络流量是系统管理员必需处理的最棘手工作之一,我们必需规定连接系统的用户满足防火墙的传入和传出要求,以最大限度保证系统免受攻击.很多用户把 Linux ...

  6. Vim as a Python IDE

    参考视频:http://v.youku.com/v_show/id_XNDY4NTM4NzY0.html 好的,在我们默认的centos6的操作系统中使用的python2,我们一般会再去安装一个pyt ...

  7. B站视频下载(VideoHelper)

    继续上次的知乎爬虫, 这次开始了哔哩哔哩的爬虫实践: 首先介绍下如何下载吧: VideoHelper 里面有三种方式下载b站视频. 同样的流程, 还是先抓包,分析参数,寻找参数(包括之前的请求包和页面 ...

  8. ElasticSearch:集群(Cluster),节点(Node),分片(Shard),Indices(索引),replicas(备份)之间关系

    [Cluster]集群,一个ES集群由一个或多个节点(Node)组成,每个集群都有一个cluster name作为标识----------------------------------------- ...

  9. import java.util.Collections类

    Collections类提供了一些操作集合的方法  下面介绍几个方法 1.将集合变为线程安全的 三个方法分别对应了ArrayList,HashMap,HashSet: Collections.sync ...

  10. MAC 下安装RabbitMQ

    1.使用brew来安装 RabbitMQ(地址:http://www.rabbitmq.com/install-standalone-mac.html ) 2.安装目录 /usr/local/Cell ...