题目连接:http://codeforces.com/contest/723/problem/D

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*********题目大意:给定一个矩阵,"*"代表陆地,"."代表水。定义被陆地包围且不与边界连接的水为湖泊,与边界连接就算为海洋,连续的水域算一片湖泊。要求填住一定量的湖泊,使得剩下的湖泊数量为要求的值,求最少要填多少面积(一格面积为1)以及填完后的矩阵。解题思路:要填湖,先要找湖,而找湖的过程是很典型的dfs的应用,寻找连续水域,所以dfs解决,并将湖泊的信息(面积,起始点)存在数组中。然后要求最少面积,直接贪心解决,把之前得到的湖泊的信息从小到大排序,逐个删除,使剩余湖泊数量达到要求即可。删除方式为再跑一遍dfs。

代码如下:
#include<bits/stdc++.h>

using namespace std;

struct data
{
    int a,b;
    int res;
};

,ans=;
;
][];
][]= {};
data ss[];
]= {,,,-,,};
]= {,,,,-,};

bool comp(const data &a,const data &b)
{
    return a.res<b.res;
}

void dfs(int x,int y)
{
    ||x==m-||y==||y==n-))
    {
        flag=;
        return;
    }
    vis[x][y]=;
    ans++;
    ; u<=; u++)
    {
        int a=x+fx[u];
        int b=y+fy[u];
        if(g[a][b]!='*'&&!vis[a][b])
            dfs(a,b);
    }
}
void dfs1(int x,int y)
{
    if(g[x][y]=='*')
        return;
    ||x==m-||y==||y==n-)
        return;
    g[x][y]='*';
    ; u<=; u++)
    {
        int a=x+fx[u];
        int b=y+fy[u];
        dfs1(a,b);
    }
}

int main()
{
    ];
    ;
    scanf("%d%d%d",&m,&n,&k);
    ; i<m; i++)
    {
        scanf("%s",a);
        ; j<n; j++)
            g[i][j]=a[j];
    }
    ; i<m-; i++)
    {
        ; j<n-; j++)
        {
            if(g[i][j]!='*'&&!vis[i][j])
            {
                flag=;
                ans=;
                dfs(i,j);
                if(flag)
                {
                    ss[sum].a=i;
                    ss[sum].b=j;
                    ss[sum].res=ans;
                    sum++;
                }
            }
        }
    }
    sort(ss,ss+sum,comp);
    sum=sum-k;
    ; i<sum; i++)
    {
        dfs1(ss[i].a,ss[i].b);
        r+=ss[i].res;
    }
    cout<<r<<endl;
    ; i<m; i++)
    {
        ; j<n; j++)
            cout<<g[i][j];
        cout<<endl;
    }
}

最近一直在做dfs,提高自己对于递归过程的思维能力,但是迄今为止仍觉得不足,需要更努力才行,加油干吧。

codeforces-723D的更多相关文章

  1. codeforces 723D(DFS)

    题目链接:http://codeforces.com/problemset/problem/723/D 题意:n*m的矩阵中,'*'代表陆地,'.'代表水,连在一起且不沿海的水形成湖泊.问最少填多少块 ...

  2. 【Codeforces 723D】Lakes in Berland (dfs)

    海洋包围的小岛,岛内的有湖,'.'代表水,'*'代表陆地,给出的n*m的地图里至少有k个湖,求填掉面积尽量少的水,使得湖的数量正好为k. dfs找出所有水联通块,判断一下是否是湖(海水区非湖).将湖按 ...

  3. codeforces 723D: Lakes in Berland

    Description The map of Berland is a rectangle of the size n × m, which consists of cells of size 1 × ...

  4. Codeforces 723d [暴力dfs]

    /* 不要低头,不要放弃,不要气馁,不要慌张. 题意: 给n,m和k,n和m为所给矩阵的高和宽.k是要求最多剩下的湖的数量. 在所给的矩阵中,*代表陆地,.代表水. 湖的定义是一片连续的水(上下左右四 ...

  5. CodeForces 723D Lakes in Berland (dfs搜索)

    题意:给定一个n*m的矩阵,*表示陆地, . 表示水,一些连通的水且不在边界表示湖,让你填最少的陆地使得图中湖剩下恰好为k. 析:很简单的一个搜索题,搜两次,第一次把每个湖的位置和连通块的数量记下来, ...

  6. Codeforces 723D. Lakes in Berland

    解题思路: 1.dfs所有的水,顺便计数大小并判断是不是湖. 2.如果是湖,将大小和坐标存下来. 3.对湖按大小从小到大排序. 4.dfs前(湖的数量-k)个湖,用*填充这些湖. 代码: #inclu ...

  7. 【29.70%】【codeforces 723D】Lakes in Berland

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  8. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  9. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  10. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

随机推荐

  1. 每天一个Linux命令(2):shutdown命令

    shutdown命令是系统关机命令.shutdown指令可以关闭所有程序,并依用户的需要,进行重新开机或关机的动作. 语法 shutdown(选项)(参数) 选项 -c:取消已经在进行的 shutdo ...

  2. 孤荷凌寒自学python第三十二天python的代码块中的异常的捕获

    孤荷凌寒自学python第三十二天python的代码块中的异常的捕获 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 今天简单了解了Python的错误陷阱,了解到其与过去学过的其它语言非常类似 ...

  3. Java 以及JEE环境快速搭建

    吐槽一下 博主最近找了一个Java Development的实习,加上上个月末的考试周,所以很久没有更新博客. 上了一周的班,还没有在熟悉项目的阶段. 感想:哇,读别人的代码是一件很费力的事情啊!!! ...

  4. spring security注解(1)

    Chapter 15. 基于表达式的权限控制 Spring Security 3.0介绍了使用Spring EL表达式的能力,作为一种验证机制 添加简单的配置属性的使用和访问决策投票,就像以前一样. ...

  5. 内存检测工具valgrind

    valgrind --tool=memcheck --leak-check=full --error-limit=no  --trace-children=yes  ./server valgrind ...

  6. BZOJ3244 [Noi2013]树的计数 【数学期望 + 树遍历】

    题目链接 BZOJ3244 题解 不会做orz 我们要挖掘出\(bfs\)序和\(dfs\)序的性质 ①容易知道\(bfs\)序一定是一层一层的,如果我们能确定在\(bfs\)序中各层的断点,就能确定 ...

  7. 虚拟机——vmtools安装出现Detected GCC binary at usr.bin.gcc.

    在安装VMWare Tools遇到过这样一个问题 Searching for GCC... Detected GCC binary at "/usr/bin/gcc". The p ...

  8. DHTMLX 表格组件(dhtmlxGrid )使用介绍

    String sql = select {s.*} from t_student s where s.age22; SQLQuery slqQuery = session.createSQLQuery ...

  9. EAR、JAR、WAR(IT)

    EAR文件包括整个项目,内含多个ejb module(jar文件)和web module(war文件)   JAR.WAR.EAR.在文件结构上,三者并没有什么不同,它们都采用zip或jar档案文件压 ...

  10. 部分浏览器上a标签包裹的dom元素显示不正常

    在苹果和部分安卓机上出现,pc端和chrome浏览器响应式设计里怎么样也不会出现的访问后a标签包裹的dom元素显示不正常a标签内的hr元素颜色显示不正常hr水平线的颜色被 bootstrap的css的 ...