CodeForces

来源:互联网 发布:软件项目质量管理问题 编辑:程序博客网 时间:2024/06/14 00:42

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

这里写图片描述

Hint

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个湖 要求填掉的湖的面积最小 一格面积是1

AC代码

#include<cstdio>#include<cstring>#include<stack>#include <set>#include <queue>#include <vector>#include<iostream>#include<algorithm>using namespace std;typedef long long LL;char st[60][60];int vis[60][60];int f[3600];vector <pair<int, int> > v;int n,m,k;int to[2][4] = {1,0,-1,0,0,1,0,-1};int sea;int tot = 1,cnt;/*cnt 湖泊大小 tot第几个*/bool dfs(int x,int y){    cnt++;    vis[x][y] = tot;    if (x==0||x==n-1||y==0||y==m-1) sea = 1;    for (int i = 0;i < 4; ++i){        int tx = x + to[0][i];        int ty = y + to[1][i];        if (tx<0||tx>=n||ty<0||ty>=m||st[tx][ty]!='.'||vis[tx][ty]) continue;        dfs(tx,ty);    }}int main(){    scanf("%d%d%d",&n,&m,&k);    for (int i = 0;i < n; ++i){        scanf("%s",st[i]);    }    for (int i = 0;i < n; ++i){        for (int j = 0;j < m; ++j){            if (st[i][j]=='.'&&!vis[i][j]){                sea = cnt = 0;                ++tot;                dfs(i,j);                if (!sea) v.push_back({cnt,tot});            }        }    }    sort(v.begin(),v.end());    int sum = 0;    int j = 0;    for (int i = v.size(); i > k; --i){        f[v[j].second] = 1; sum += v[j].first;        j++;    }    printf("%d\n",sum);    for (int i = 0;i < n; ++i){        for (int j = 0;j < m; ++j){            if (f[vis[i][j]]) printf("*");            else printf("%c",st[i][j]);        }        printf("\n");    }    return 0;}
原创粉丝点击