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. PIE SDK矢量自定义渲染

    1. 功能简介 PIE SDK中关于矢量渲染提供了多种方案,包括简单渲染.分级渲染.唯一值渲染,这几种渲染方式具有一定的通用性,可以满足用户绝大多数的需求. 当面对复杂的业务,当前渲染方案无法满足用户 ...

  2. Node.js frameworks

    1. Express 2. Koa 3. LoopBack egghead.io What is egghead? egghead is a group of working web developm ...

  3. 打ms15-034补丁出现“此更新 不适用于您的计算机”

    1.MS15-034漏洞的补丁是KB3042553; 2.如果在一台Windows Server 2012 R2的服务器上直接安装补丁文件KB3042553,可能会出现“此更新 不适用于您的计算机”的 ...

  4. Redis在windows下的配置

    Redis在windows下的配置(在windows-64下安装redis,请参考微软redis的github:https://github.com/MSOpenTech/redis/releases ...

  5. spring初始化bean的目的

    初始化bean就是为了将所有需要的bean全部装载到容器里面,等我们需要用到哪个bean就将哪个bean从容器里面拿出来

  6. TOJ 4002 Palindrome Generator

    描述 A palindrome is a number that reads the same whether you read it from left to right or from right ...

  7. 工作采坑札记:4. Hadoop获取InputSplit文件信息

    1. 场景 基于客户的数据处理需求,客户分发诸多小数据文件,文件每行代表一条记录信息,且每个文件以"类型_yyyyMMdd_批次号"命名.由于同一条记录可能存在于多个文件中,且处于 ...

  8. 白话SpringCloud | 第十一章:路由网关(Zuul):利用swagger2聚合API文档

    前言 通过之前的两篇文章,可以简单的搭建一个路由网关了.而我们知道,现在都奉行前后端分离开发,前后端开发的沟通成本就增加了,所以一般上我们都是通过swagger进行api文档生成的.现在由于使用了统一 ...

  9. 【linux】dpkg 命令使用说明

    dpkg是一个debian包管理工具.能够对包进行安装.卸载.获取信息等操作.用法:    安装(解包并配置):       dpkg -i package_file       dpkg --ins ...

  10. java去掉String里面的空格、换行符等

    package com.ynet.utils; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * Create ...