题目连接: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,提高自己对于递归过程的思维能力,但是迄今为止仍觉得不足,需要更努力才行,加油干吧。

最新文章

  1. perl 切换 dnspod 域名记录
  2. [Maven] 变态问题收集
  3. linux在home目录下使用ls命令卡死
  4. SharePoint Server 2013开发之旅(四):配置工作流开发和测试环境
  5. Map遍历两种方式
  6. Android 手机卫士--解析json与消息机制发送不同类型消息
  7. CSS Reset样式重置
  8. eclipse连接虚拟机
  9. ios-序列帧动画核心代码简单介绍以及封装
  10. Android Activity学习笔记(一)
  11. iOS10全新推送功能的实现
  12. .net在Controller里的方法添加[HttpGet]和[HttpPost]
  13. Oracle外部表详解(转载)
  14. cocos2d-js Mac下的JSB绑定步骤
  15. Spring核心框架 - AOP的起源及介绍
  16. Linux编程之自定义消息队列
  17. easyui message show中msg嵌入一个按钮如何绑定事件
  18. 利用Needleman–Wunsch算法进行DNA序列全局比对
  19. Django web框架-----Django连接本地现有mysql数据库
  20. 在关闭页面时自动清除Session cookie,页面缓存

热门文章

  1. PowerShell技巧:使用XPath语法查询XML文件
  2. react事件处理及动态样式添加
  3. Python&#160;lambda介绍
  4. WIN10把照片查看器设为默认看图软件
  5. 【转载】Unity3D研究院之IOS&amp;Andoird使用Sharesdk遇到的坑
  6. JS 如何获取radio或者checkbox选中后的值
  7. shell中的&gt;&amp;1和 &gt;&amp;2是什么意思?
  8. ALPHA(10)
  9. OpenFlow-Enaling innvation in Campus Networks
  10. java 中基本类型与字符串之间的互相转换