D. Lakes in Berland

题目连接:

http://codeforces.com/contest/723/problem/D

Description

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.

Sample Input

5 4 1
****
..
****
**.*
..**

Sample Output

1
****
..
****
****
..**

Hint

题意

给你一个n/*m的矩阵,然后你们有不少于k条河流,然后你需要使得一些河流变成陆地,使得河流的数量恰好等于k,问你至少填多少个水。

河流的定义是水塘,且不与外界相连的地方。

题解:

直接dfs搜出每一条河流,然后贪心排序,从小到大去填满就好了。

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 55;
int n,m,k;
string s[maxn];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
int vis[maxn][maxn];
int sp[maxn*maxn],cnt,area,flag;
void dfs(int x,int y)
{
    area++;
    vis[x][y]=1;
    if(x==0||x==n-1||y==0||y==m-1)flag=1;
    for(int i=0;i<4;i++)
    {
        int nx=x+dx[i];
        int ny=y+dy[i];
        if(nx<0||nx>=n)continue;
        if(ny<0||ny>=m)continue;
        if(s[nx][ny]=='*')continue;
        if(vis[nx][ny])continue;
        dfs(nx,ny);
    }
}
struct node
{
    int a,b,c;
}t[5000];
bool cmp(node a,node b)
{
    return a.a<b.a;
}
void dfs2(int x,int y)
{
    s[x][y]='*';
    for(int i=0;i<4;i++)
    {
        int nx=x+dx[i];
        int ny=y+dy[i];
        if(nx<0||nx>=n)continue;
        if(ny<0||ny>=m)continue;
        if(s[nx][ny]=='*')continue;
        dfs2(nx,ny);
    }
}
int main()
{
    scanf("%d%d%d",&n,&m,&k);
    for(int i=0;i<n;i++)cin>>s[i];
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(!vis[i][j]&&s[i][j]=='.')
            {
                area=0;
                flag=0;
                dfs(i,j);
                if(flag==1)continue;
                t[cnt].a=area,t[cnt].b=i,t[cnt].c=j;
                cnt++;
            }
        }
    }
    sort(t,t+cnt,cmp);
    int aaans = 0;
    for(int i=0;i<cnt-k;i++)
    {
        aaans+=t[i].a;
        dfs2(t[i].b,t[i].c);
    }
    cout<<aaans<<endl;
    for(int i=0;i<n;i++)
        cout<<s[i]<<endl;
}

Codeforces Round #375 (Div. 2) D. Lakes in Berland 贪心的更多相关文章

  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 dfs

    D. Lakes in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  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 #297 (Div. 2)C. Ilya and Sticks 贪心

    Codeforces Round #297 (Div. 2)C. Ilya and Sticks Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: xxx  ...

  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 #346 (Div. 2) C. Tanya and Toys 贪心

    C. Tanya and Toys 题目连接: http://www.codeforces.com/contest/659/problem/C Description In Berland recen ...

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

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

随机推荐

  1. iOS支付宝支付总结

    1.按照http://doc.open.alipay.com/doc2/detail?spm=0.0.0.0.SWdJgo&treeId=59&articleId=103676& ...

  2. 初识linux

    1.版本 稳定版本:偶数版如2.6.X 发展中的版本:奇数版如2.5.X linux distribution包含:linux kernel + free software + documentati ...

  3. android studio插件提升工作效率

    SonarLint 代码质量管理插件 ButterKnife Zelezny ButterKnife 生成器,快速根据布局文件生成属性对象. SelectorChapek 设计师给我们提供好了各种资源 ...

  4. QMainWindow的setLayout的问题

    因为QMainWindow有自己的layout,所以需要一个QWidget,然后setCentralWidget,给这个QWidget调用setLayout http://stackoverflow. ...

  5. 《Java数据结构与算法》笔记-CH5-链表-3双端链表

    /** * 双端链表的实现 */ class LinkA { public long dData; public LinkA next; public LinkA(long d) { dData = ...

  6. [HIve - LanguageManual] Hive Operators and User-Defined Functions (UDFs)

    Hive Operators and User-Defined Functions (UDFs) Hive Operators and User-Defined Functions (UDFs) Bu ...

  7. [02] Oracle简单单行函数(字符+数值+日期+转换+通用)

    1. 字符函数 --upper(str), lower(str):将str转换成大,小写 select upper('hello') as x1, lower('HELlo') as x2 from ...

  8. Smarty 使用继承方式实现配置

    . 常用配置选项 在使用Smarty模板引擎之前,我们必须先学习如何配置Smarty的选项.而在Smarty的常见选项中,我们首先必须了解4个最基本的目录选项. 模板目录(template):本目录用 ...

  9. [置顶] Hibernate从入门到精通(十)多对多单向关联映射

    上一篇文章Hibernate从入门到精通(九)一对多双向关联映射中我们讲解了一下关于一对多关联映射的相关内容,这次我们继续多对多单向关联映射. 多对多单向关联映射 在讲解多对多单向关联映射之前,首先看 ...

  10. 第四章 Struts2深入

    4.1 Struts2架构    1.ActionMapper:        提供请求和Action之间的映射.根据请求查找是否存在对于的action,如有,翻译描述action映射的ActionM ...