目录

CF525D-Arthur and Walls

CF525D-Arthur and Walls

题目:

题目描述:

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.

输入格式:

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 $ 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.

样例:

样例输入1:

1
2
3
4
5
6
5 5
.*.*.
*****
.*.*.
*****
.*.*.

样例输出1:

1
2
3
4
5
.*.*.
*****
.*.*.
*****
.*.*.

样例输入2:

1
2
3
4
5
6
7
6 7
***.*.*
..*.*.*
*.*.*.*
*.*.*.*
..*...*
*******

样例输出2:

1
2
3
4
5
6
***...*
..*...*
..*...*
..*...*
..*...*
*******

样例输入3:

1
2
3
4
5
4 5
.....
.....
..***
..*..

样例输出3:

1
2
3
4
.....
.....
.....
.....

思路:

实现:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "ybwhead/ios.h"

int n, m;

char cc[5000][5000];

queue<pair<int, int>> q;

int dx[] = {1, -1, 0, 0, 1, -1, 1, -1};
int dy[] = {0, 0, 1, -1, 1, -1, -1, 1};

bool check(int i, int j)
{
    if (i < 1 || i > n || j < 1 || j > m)
        return 0;
    if ((cc[i - 1][j] == '.' && cc[i][j - 1] == '.' && cc[i - 1][j - 1] == '.') || (cc[i + 1][j] == '.' && cc[i][j - 1] == '.' && cc[i + 1][j - 1] == '.') || (cc[i - 1][j] == '.' && cc[i][j + 1] == '.' && cc[i - 1][j + 1] == '.') || (cc[i + 1][j] == '.' && cc[i][j + 1] == '.' && cc[i + 1][j + 1] == '.'))
        return 1;
    return 0;
}

int main()
{
    yin >> n >> m;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
        {
            yin >> cc[i][j];
            if (cc[i][j] == '.')
                q.push(make_pair(i, j));
        }
    while (!q.empty())
    {
        int i = q.front().first, j = q.front().second;
        q.pop();
        for (int now = 0; now < 8; now++)
        {
            if (cc[i + dx[now]][j + dy[now]] == '*' && check(i + dx[now], j + dy[now]))
            {
                cc[i + dx[now]][j + dy[now]] = '.';
                q.push(make_pair(i + dx[now], j + dy[now]));
            }
        }
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
            yout << cc[i][j];
        yout << "\n";
    }
}