codeforces 723D Lakes in Berland

来源:互联网 发布:联想服务器数据恢复 编辑:程序博客网 时间:2024/05/16 11:06
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 ≤ 500 ≤ 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.

题意:这题目很好理解,给你给定一个n*m的格子,'*'代表陆地 ,'.'代表水池,如果水池位于格子的边上认为水和海洋相连,如果水池相互共边责任为是连通的则认为是一个水池(面积大而已),现在想获得K个水池,问你需要转换多少个水池格子为陆地(即水池面积)

思路:深搜,判断水池的位置,记录每个水池所连通的所有位置(位置多少代表面积多大),然后进行处理,每次处理的时候从水池面积最小的开始处理

代码:

#include <iostream>#include <vector>#include <cstdio>#include <cstdlib>#include <set>#include <map>#include <queue>#include <algorithm>#include <cstring>using namespace std;int n,m,k;char gra[105][105];vector< pair<int,int> >v[3000];int ct;int judge(int x,int y) {//判断水池是否与海洋相连    if(x == 0||x == n-1 || y == 0 || y==m-1) {        if(gra[x][y] == '.') {            return 1;        }    }    return 0;}int flag;int xx[4] = {0,0,1,-1};int yy[4] = {1,-1,0,0};bool used[55][55];vector< pair<int,int> >vt;void dfs(int x,int y) {    if(x < 0 || y < 0 ||x >= n || y >= m){        return;    }    if(gra[x][y] != '.' || used[x][y]){        return;    }    if(judge(x,y)){        flag = 1;        return;    }    used[x][y] = true;    vt.push_back(pair<int,int>(x,y));    for(int i = 0; i < 4; i++) {        int x1 = x+xx[i];        int y1 = y+yy[i];        dfs(x1,y1);    }}bool cmp(vector< pair<int,int> >a, vector< pair<int,int> > b) {    return a.size() < b.size();}int main() {    cin >> n >> m >> k;    for(int i = 0; i < n; i++) {        cin >> gra[i];    }    ct = 0;    memset(used,false,sizeof(used));    for(int i = 0; i < n; i++) {        for(int j = 0; j < m; j++) {            if(gra[i][j] == '.' && !used[i][j]) {                flag = 0;                vt.clear();//记录每个水池连通的所有位置                dfs(i,j);                if(!flag){                    v[ct] = vt;                }                ct++;            }        }    }    sort(v,v+ct,cmp);//按照面积大小进行排序    int st = 0;    int ans = 0;    while(st < ct-k){        int len = v[st].size();        ans += len;        for(int i = 0;i < len;i++){//处理每一个水池            gra[v[st][i].first][v[st][i].second] = '*';        }        st++;    }    cout <<ans <<endl;    for(int i = 0; i < n; i++) {        cout << gra[i] << endl;    }    return 0;}


0 0
原创粉丝点击