Codeforces Round #375 (Div. 2)——D. Lakes in Berland(DFS连通块)

时间:2022-05-01 23:05:24
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 nm 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.

题目链接:D. Lakes in Berland

题意就是给一个n*m的地图,其中边缘地区邻接着海,不能算一个湖,里面的每一个连通块都算一个湖,求最少填掉多少块小格子使得湖的数量变成k(起始湖数量大于等于k)

没见过这么水的D题……先DFS求不邻接大海的连通块(这里如果用并查集求连通块就判断邻接大海就没有DFS这么简便),然后把每一块中DFS的起始点坐标和湖的面积组成结构体记录并按照湖的面积排序,再贪心地再写一个DFS填充面积小的湖,然而地没注释调试信息和调用的函数名字搞错WA了很多次

代码:

#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=55;
struct info
{
int need;
int x,y;
bool operator<(const info &t)const
{
return need>t.need;
}
};
priority_queue<info>Q; char pos[N][N];
int vis[N][N];
int n,m;
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int is_lake,once; inline bool check(int x,int y)
{
return x>=0&&x<n&&y>=0&&y<m&&pos[x][y]=='.'&&!vis[x][y];
}
void dfs(int x,int y)
{
vis[x][y]=1;
++once;
if(x==0||y==0||x==n-1||y==m-1)
is_lake=0;
for (int i=0; i<4; ++i)
{
int xx=x+dir[i][0];
int yy=y+dir[i][1];
if(check(xx,yy))
dfs(xx,yy);
}
}
void kuosan(int x,int y)
{
pos[x][y]='*';
for (int i=0; i<4; ++i)
{
int xx=x+dir[i][0];
int yy=y+dir[i][1];
if(xx>=0&&xx<n&&yy>=0&&yy<m&&pos[xx][yy]=='.')
kuosan(xx,yy);
}
}
int main(void)
{
int i,j,k;
while (~scanf("%d%d%d",&n,&m,&k))
{
CLR(vis,0);
for (i=0; i<n; ++i)
scanf("%s",pos[i]);
int sc=0;
for (i=0; i<n; ++i)
{
for (j=0; j<m; ++j)
{
if(pos[i][j]=='.'&&!vis[i][j])
{
is_lake=1;
once=0;
dfs(i,j);
if(is_lake)
Q.push((info){once,i,j}),++sc;
}
}
}
int r=0;
info now;
while (sc>k)
{
now=Q.top();
Q.pop();
kuosan(now.x,now.y);
r+=now.need;
--sc;
}
printf("%d\n",r);
for (i=0; i<n; ++i)
printf("%s\n",pos[i]);
}
return 0;
}

Codeforces Round #375 (Div. 2)——D. Lakes in Berland(DFS连通块)的更多相关文章

  1. Codeforces Round &num;375 &lpar;Div&period; 2&rpar; D&period; 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 &num;375 &lpar;Div&period; 2&rpar; D&period; Lakes in Berland 贪心

    D. Lakes in Berland 题目连接: http://codeforces.com/contest/723/problem/D Description The map of Berland ...

  3. Codeforces Round &num;375 &lpar;Div&period; 2&rpar; D&period; 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 &num;375 &lpar;Div&period; 2&rpar; D&period; Lakes in Berland 并查集

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

  5. Codeforces Round &num;375 &lpar;Div&period; 2&rpar;

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

  6. Codeforces Round &num;375 &lpar;Div&period; 2&rpar; - D

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

  7. Codeforces Round &num;375 &lpar;Div&period; 2&rpar; ABCDE

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

  8. Codeforces Round &num;375 &lpar;Div&period; 2&rpar; F&period; st-Spanning Tree 生成树

    F. st-Spanning Tree 题目连接: http://codeforces.com/contest/723/problem/F Description You are given an u ...

  9. Codeforces Round &num;375 &lpar;Div&period; 2&rpar; - C

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

随机推荐

  1. css3 transition属性

    最近打算学习css3知识,觉得css3写出来的效果好炫好酷,之前一直想要学习来着.可能之前的决心,毅力,耐心不够,所以想要重整起来,放下浮躁的心态,一步一个脚印,踏踏实实的来学习. 首先学习的是css ...

  2. css兼容问题

    Opacity的兼容处理          filter:alpha(opacity=100); /* IE */        opacity: 0.5; /* 支持opacity的浏览器*/

  3. javascript --- 实时监听输入框值的变化

    实时监听文本框值变化是非常常见的功能,通常最简单的办法就是用keyup,keydown来实现,但是这种方法有两个问题,一个是当直接复制粘贴的时候没法监听到事件,另外一个问题是在移动端,使用删除键删除输 ...

  4. 3-4 rpm包查询

    概述:yum不能查询已经安装好的rpm包, 就算采用了yum来进行安装,查询方法还是依赖rpm包的查询, 因此rpm包的查询十分常用和重要 1.查询是否安装 <1>rpm -q 包名(不是 ...

  5. 20160402javaweb 开发模式

    开发案例: 首先,我们确定用xml文件代替数据库,便于测试 建立web工程,基本架构见下图 代码如下: 首先是javabean:User.java package com.dzq.domian; im ...

  6. Lambda Expression In Java

     题记在阅读JDK源码java.util.Collections的时候在UnmodifiableCollection类中看到了这么一段代码: public void forEach(Consumer& ...

  7. Matrix&lpar;线段树版&rpar;

    poj2155:http://poj.org/problem?id=2155 题意;同上一遍随笔. 题解:这里用二维线段树打了一发.第一次学习别人的代码.才学的.这种树套树的程序,确实很费脑子,一不小 ...

  8. XML &lpar;DOM解析&rpar; 看看就行

    000000000000000000000000000000000000000 ------------------------------------------------------------ ...

  9. &lbrack;转载&rsqb; 常用 Java 静态代码分析工具的分析与比较

    转载自http://www.oschina.net/question/129540_23043 简介: 本文首先介绍了静态代码分析的基本概念及主要技术,随后分别介绍了现有 4 种主流 Java 静态代 ...

  10. &period;NET方法无限传参数技术

    是否有这样的需求在创建函数时参数个数不固定,又不想使用重载,那么下面这个技术就比较适合. 相信你一定见过下面这的代码: ); Format 就是string的一个函数,第一个参数是固定的字符串类型,那 ...