您的位置:首页 > 其它

D. Arthur and Walls(Codeforces Round #297 (Div. 2) 搜索(BFS))

2015-04-01 21:25 253 查看
D. Arthur and Walls

time limit per test
2 seconds

memory limit per test
512 megabytes

input
standard input

output
standard output

Finally it is a day when Arthur has enough money for buying an apartment. He found a great option close to the center of the city with a nice price.

Plan of the apartment found by Arthur looks like a rectangle n × m consisting of squares of size 1 × 1.
Each of those squares contains either a wall (such square is denoted by a symbol "*" on the plan) or a free space (such square is denoted on the plan by a symbol
".").

Room in an apartment is a maximal connected area consisting of free squares. Squares are considered adjacent if they share a common side.

The old Arthur dream is to live in an apartment where all rooms are rectangles. He asks you to calculate minimum number of walls you need to remove in order to achieve this goal. After removing a wall from a square it becomes a free square. While removing the
walls it is possible that some rooms unite into a single one.

Input

The first line of the input contains two integers n, m (1 ≤ n, m ≤ 2000)
denoting the size of the Arthur apartments.

Following n lines each contain m symbols — the plan
of the apartment.

If the cell is denoted by a symbol "*" then it contains a wall.

If the cell is denoted by a symbol "." then it this cell is free from walls and also this cell is contained in some of the rooms.

Output

Output n rows each consisting of m symbols that show
how the Arthur apartment plan should look like after deleting the minimum number of walls in order to make each room (maximum connected area free from walls) be a rectangle.

If there are several possible answers, output any of them.

Sample test(s)

input
5 5
.*.*.
*****
.*.*.
*****
.*.*.


output
.*.*.
*****
.*.*.
*****
.*.*.


input
6 7
***.*.*
..*.*.*
*.*.*.*
*.*.*.*
..*...*
*******


output
***...*
..*...*
..*...*
..*...*
..*...*
*******


input
4 5
.....
.....
..***
..*..


output
.....
.....
.....
.....

思路:先把地图遍历一遍,把一个2*2的方格中只有一个*的找到,存在队列中(然后把

那个*改成.),然后对队列进行处理,直到没有这样的2*2方格。输出地图
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>

using namespace std;

struct node
{
    int x;
    int y;
    //int cnt;
};

int n,m;
char map[2015][2015];
queue<node>q;

void BFS()
{
    struct node t,f;
    while(!q.empty())
    {
        t = q.front();
        q.pop();
        int i = t.x;
        int j = t.y;
        int x1,x2,x3,y1,y2,y3;
        struct node tt;
        for(int k=0; k<4; k++)
        {
            int cnt = 0;
            int ans = 0;
            if(k == 0)
            {
                x1 = i - 1;
                y1 = j;
                x2 = i - 1;
                y2 = j - 1;
                x3 = i;
                y3 = j - 1;
                if(map[i][j] == '.')
                {
                    cnt++;
                }
                else
                {
                    ans++;
                    tt.x = i;
                    tt.y = j;
                }
                if(map[x1][y1] == '.')
                {
                    cnt++;
                }
                else
                {
                    ans++;
                    tt.x = x1;
                    tt.y = y1;
                }
                if(map[x2][y2] == '.')
                {
                    cnt++;
                }
                else
                {
                    ans++;
                    tt.x = x2;
                    tt.y = y2;
                }
                if(map[x3][y3] == '.')
                {
                    cnt++;
                }
                else
                {
                    ans++;
                    tt.x = x3;
                    tt.y = y3;
                }
                if(x1<0 || x2<0 || x3 < 0 || x1>=n ||x2>=n || x3>=n || y1<0 || y2<0 || y3 < 0 || y1>=m ||y2>=m || y3>=m || cnt!=3 || ans!=1)
                {
                    continue;
                }
                map[tt.x][tt.y] = '.';
                q.push(tt);
            }
            else if(k == 1)
            {
                x1 = i;
                y1 = j + 1;
                x2 = i - 1;
                y2 = j;
                x3 = i - 1;
                y3 = j + 1;
                if(map[i][j] == '.')
                {
                    cnt++;
                }
                else
                {
                    ans++;
                    tt.x = i;
                    tt.y = j;
                }
                if(map[x1][y1] == '.')
                {
                    cnt++;
                }
                else
                {
                    ans++;
                    tt.x = x1;
                    tt.y = y1;
                }
                if(map[x2][y2] == '.')
                {
                    cnt++;
                }
                else
                {
                    ans++;
                    tt.x = x2;
                    tt.y = y2;
                }
                if(map[x3][y3] == '.')
                {
                    cnt++;
                }
                else
                {
                    ans++;
                    tt.x = x3;
                    tt.y = y3;
                }
                if(x1<0 || x2<0 || x3 < 0 || x1>=n ||x2>=n || x3>=n || y1<0 || y2<0 || y3 < 0 || y1>=m ||y2>=m || y3>=m || cnt!=3 || ans!=1)
                {
                    continue;
                }
                map[tt.x][tt.y] = '.';
                q.push(tt);
            }
            else if(k == 2)
            {
                x1 = i;
                y1 = j - 1;
                x2 = i + 1;
                y2 = j - 1;
                x3 = i + 1;
                y3 = j;
                if(map[i][j] == '.')
                {
                    cnt++;
                }
                else
                {
                    ans++;
                    tt.x = i;
                    tt.y = j;
                }
                if(map[x1][y1] == '.')
                {
                    cnt++;
                }
                else
                {
                    ans++;
                    tt.x = x1;
                    tt.y = y1;
                }
                if(map[x2][y2] == '.')
                {
                    cnt++;
                }
                else
                {
                    ans++;
                    tt.x = x2;
                    tt.y = y2;
                }
                if(map[x3][y3] == '.')
                {
                    cnt++;
                }
                else
                {
                    ans++;
                    tt.x = x3;
                    tt.y = y3;
                }
                if(x1<0 || x2<0 || x3 < 0 || x1>=n ||x2>=n || x3>=n || y1<0 || y2<0 || y3 < 0 || y1>=m ||y2>=m || y3>=m || cnt!=3 || ans!=1)
                {
                    continue;
                }
                map[tt.x][tt.y] = '.';
                q.push(tt);
            }
            else if(k == 3)
            {
                x1 = i + 1;
                y1 = j + 1;
                x2 = i + 1;
                y2 = j;
                x3 = i;
                y3 = j + 1;
                if(map[i][j] == '.')
                {
                    cnt++;
                }
                else
                {
                    ans++;
                    tt.x = i;
                    tt.y = j;
                }
                if(map[x1][y1] == '.')
                {
                    cnt++;
                }
                else
                {
                    ans++;
                    tt.x = x1;
                    tt.y = y1;
                }
                if(map[x2][y2] == '.')
                {
                    cnt++;
                }
                else
                {
                    ans++;
                    tt.x = x2;
                    tt.y = y2;
                }
                if(map[x3][y3] == '.')
                {
                    cnt++;
                }
                else
                {
                    ans++;
                    tt.x = x3;
                    tt.y = y3;
                }
                if(x1<0 || x2<0 || x3 < 0 || x1>=n ||x2>=n || x3>=n || y1<0 || y2<0 || y3 < 0 || y1>=m ||y2>=m || y3>=m || cnt!=3 || ans!=1)
                {
                    continue;
                }
                map[tt.x][tt.y] = '.';
                q.push(tt);
            }
        }
    }
    for(int i=0; i<n; i++)
    {
        printf("%s\n",map[i]);
    }
}

int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        while(!q.empty())
        {
            q.pop();
        }
        for(int i=0; i<n; i++)
        {
            scanf("%s",map[i]);
        }
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                int x1,x2,x3,y1,y2,y3;
                struct node tt;
                for(int k=0; k<4; k++)
                {
                    int cnt = 0;
                    int ans = 0;
                    if(k == 0)
                    {
                        x1 = i - 1;
                        y1 = j;
                        x2 = i - 1;
                        y2 = j - 1;
                        x3 = i;
                        y3 = j - 1;
                        if(map[i][j] == '.')
                        {
                            cnt++;
                        }
                        else
                        {
                            ans++;
                            tt.x = i;
                            tt.y = j;
                        }
                        if(map[x1][y1] == '.')
                        {
                            cnt++;
                        }
                        else
                        {
                            ans++;
                            tt.x = x1;
                            tt.y = y1;
                        }
                        if(map[x2][y2] == '.')
                        {
                            cnt++;
                        }
                        else
                        {
                            ans++;
                            tt.x = x2;
                            tt.y = y2;
                        }
                        if(map[x3][y3] == '.')
                        {
                            cnt++;
                        }
                        else
                        {
                            ans++;
                            tt.x = x3;
                            tt.y = y3;
                        }
                        if(x1<0 || x2<0 || x3 < 0 || x1>=n ||x2>=n || x3>=n || y1<0 || y2<0 || y3 < 0 || y1>=m ||y2>=m || y3>=m || cnt!=3 || ans!=1)
                        {
                            continue;
                        }
                        map[tt.x][tt.y] = '.';
                        q.push(tt);
                    }
                    else if(k == 1)
                    {
                        x1 = i;
                        y1 = j + 1;
                        x2 = i - 1;
                        y2 = j;
                        x3 = i - 1;
                        y3 = j + 1;
                        if(map[i][j] == '.')
                        {
                            cnt++;
                        }
                        else
                        {
                            ans++;
                            tt.x = i;
                            tt.y = j;
                        }
                        if(map[x1][y1] == '.')
                        {
                            cnt++;
                        }
                        else
                        {
                            ans++;
                            tt.x = x1;
                            tt.y = y1;
                        }
                        if(map[x2][y2] == '.')
                        {
                            cnt++;
                        }
                        else
                        {
                            ans++;
                            tt.x = x2;
                            tt.y = y2;
                        }
                        if(map[x3][y3] == '.')
                        {
                            cnt++;
                        }
                        else
                        {
                            ans++;
                            tt.x = x3;
                            tt.y = y3;
                        }
                        if(x1<0 || x2<0 || x3 < 0 || x1>=n ||x2>=n || x3>=n || y1<0 || y2<0 || y3 < 0 || y1>=m ||y2>=m || y3>=m || cnt!=3 || ans!=1)
                        {
                            continue;
                        }
                        map[tt.x][tt.y] = '.';
                        q.push(tt);
                    }
                    else if(k == 2)
                    {
                        x1 = i;
                        y1 = j - 1;
                        x2 = i + 1;
                        y2 = j - 1;
                        x3 = i + 1;
                        y3 = j;
                        if(map[i][j] == '.')
                        {
                            cnt++;
                        }
                        else
                        {
                            ans++;
                            tt.x = i;
                            tt.y = j;
                        }
                        if(map[x1][y1] == '.')
                        {
                            cnt++;
                        }
                        else
                        {
                            ans++;
                            tt.x = x1;
                            tt.y = y1;
                        }
                        if(map[x2][y2] == '.')
                        {
                            cnt++;
                        }
                        else
                        {
                            ans++;
                            tt.x = x2;
                            tt.y = y2;
                        }
                        if(map[x3][y3] == '.')
                        {
                            cnt++;
                        }
                        else
                        {
                            ans++;
                            tt.x = x3;
                            tt.y = y3;
                        }
                        if(x1<0 || x2<0 || x3 < 0 || x1>=n ||x2>=n || x3>=n || y1<0 || y2<0 || y3 < 0 || y1>=m ||y2>=m || y3>=m || cnt!=3 || ans!=1)
                        {
                            continue;
                        }
                        map[tt.x][tt.y] = '.';
                        q.push(tt);
                    }
                    else if(k == 3)
                    {
                        x1 = i + 1;
                        y1 = j + 1;
                        x2 = i + 1;
                        y2 = j;
                        x3 = i;
                        y3 = j + 1;
                        if(map[i][j] == '.')
                        {
                            cnt++;
                        }
                        else
                        {
                            ans++;
                            tt.x = i;
                            tt.y = j;
                        }
                        if(map[x1][y1] == '.')
                        {
                            cnt++;
                        }
                        else
                        {
                            ans++;
                            tt.x = x1;
                            tt.y = y1;
                        }
                        if(map[x2][y2] == '.')
                        {
                            cnt++;
                        }
                        else
                        {
                            ans++;
                            tt.x = x2;
                            tt.y = y2;
                        }
                        if(map[x3][y3] == '.')
                        {
                            cnt++;
                        }
                        else
                        {
                            ans++;
                            tt.x = x3;
                            tt.y = y3;
                        }
                        if(x1<0 || x2<0 || x3 < 0 || x1>=n ||x2>=n || x3>=n || y1<0 || y2<0 || y3 < 0 || y1>=m ||y2>=m || y3>=m || cnt!=3 || ans!=1)
                        {
                            continue;
                        }
                        map[tt.x][tt.y] = '.';
                        q.push(tt);
                    }
                }
            }
        }
        BFS();
    }
    return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: