codeforce#375D. Lakes in Berland

来源:互联网 发布:惠普1513驱动for mac 编辑:程序博客网 时间:2024/05/16 04:32

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.

只是简单的bfs,但是因为自己把搜索时候的标记放在了从队列里拿出来的时候。因为这一点,想了一个早上。唉,吃一堑长一智阿。

#include<iostream>#include<algorithm>#include<cstdio>#include<cmath>#include<queue>#include<cstring>using namespace std;char a[60][60];bool vis[60][60];int nx[] = {1, 0, -1 , 0};int ny[] = {0, -1, 0, 1};int n, m;struct node{    int x;    int y;    node (int x, int y):x(x), y(y){}};struct NODE{    int x;    int y;    int sum;}rain[10000];int ans, cnt;void bfs(int i, int j){    queue<node> q;    if(a[i][j] == '.')    {        q.push(node(i, j));        while(!q.empty())        {            node tem = q.front();            q.pop();            //a[tem.x][tem.y] = '*';            vis[tem.x][tem.y] = true;            for(int kk = 0; kk < 4; kk++)            {                int tx = tem.x + nx[kk];                int ty = tem.y + ny[kk];                if(tx >= n || tx < 0 || ty >= m || ty < 0 || vis[tx][ty] || (a[tx][ty] == '*'))                    continue;                else                {                    q.push(node(tx, ty));                    vis[tx][ty] = true;                }            }        }    }}void BFS(int x, int y){    int sum = 0;    int ansx = x, ansy = y;    queue<node> q;    q.push(node(x, y));    while(!q.empty())    {        node tem = q.front();        q.pop();        sum++;        vis[tem.x][tem.y] = true;        for(int kk = 0; kk < 4; kk++)        {            int tx = tem.x + nx[kk];            int ty = tem.y + ny[kk];            if(tx >= n || tx < 0 || ty >= m || ty < 0 || vis[tx][ty] || (a[tx][ty] == '*'))                continue;            else            {                q.push(node(tx, ty));                vis[tx][ty] = true;                ansx = tx;                ansy = ty;            }        }    }    //cout << ansx+1 << " " << ansy+1 << "...\n";    rain[cnt].x = ansx;    rain[cnt].y = ansy;    rain[cnt].sum = sum;    cnt++;}void bfs2(int x, int y){    //cout << x+1 << " " << y+1 << endl;    queue<node> q;    q.push(node(x, y));    while(!q.empty())    {        node tem = q.front();        q.pop();        a[tem.x][tem.y] = '*';        vis[tem.x][tem.y] = true;        for(int kk = 0; kk < 4; kk++)        {            int tx = tem.x + nx[kk];            int ty = tem.y + ny[kk];            if(tx >= n || tx < 0 || ty >= m || ty < 0 || vis[tx][ty] || (a[tx][ty] == '*'))                continue;            else            {                q.push(node(tx, ty));                vis[tx][ty] = true;            }        }    }}bool cmp(NODE ta, NODE tb){    return ta.sum < tb.sum;}int main(){    int k;    while(cin >>n >> m >> k)    {        cnt = ans = 0;        memset(vis ,false , sizeof(vis));        for(int i = 0; i < n; i++)            cin >> a[i];        for(int j = 0; j < m; j++)        {            vis[0][j] = true;            vis[n-1][j] = true;            bfs(0, j);            bfs(n-1, j);        }        for(int i = 0; i < n; i++)        {            vis[i][0] = true;            bfs(i, 0);            vis[i][m-1] = true;            bfs(i, m-1);        }        /*cout << endl;        for(int i = 0; i < n; i++)            cout << a[i] << endl;*/        //cout << a[3][2] << endl;        for(int i = 1; i < n; i++)        {            for(int j = 1; j < m; j++)            {                if(a[i][j] == '.' && !vis[i][j])                {                    BFS(i, j);                    ans++;                }            }        }        sort(rain, rain+cnt, cmp);        //cout << cnt << endl;        //cout << rain[0].sum << endl;        int sss = 0;        memset(vis, false, sizeof(vis));        if(cnt > k)        {            int tt = cnt - k;            for(int i = 0; i < tt; i++)            {                sss += rain[i].sum;                int tx = rain[i].x;                int ty = rain[i].y;                bfs2(tx, ty);            }        }        //cout << endl;        cout << sss << endl;        for(int i = 0; i < n; i++)            cout << a[i] << endl;    }    return 0;}


0 0