D. Arthur and Walls (判断矩形)

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 rectanglen×mconsisting of squares of size1×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 integersn,m(1≤n,m≤2000) denoting the size of the Arthur apartments.

Followingnlines each containmsymbols — 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

Outputnrows each consisting ofmsymbols 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

………………..

思路:我自己一开始想的是dfs,每次搜出一个路径上的最上最下最左最右,然后在这个矩形内全赋值为‘.’,后来发现这样太慢了,各种回溯,TLE了,会有很坑爹很坑爹的数据,然后看了某些人的做法,发现很好玩,,就是去找每个2 * 2矩形上可以组成

.. ..

.* 或 *.

或者与其类似的情况,然后用队列模拟

AC代码:

#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#include <cassert>using namespace std;char a[2005][2005];int n, m;bool check(int x, int y) {if(a[x][y] == '.' || x < 1 || y < 1 || x > n || y > m) return 0;if(a[x][y – 1] == '.' && a[x – 1][y – 1] == '.' && a[x – 1][y] == '.') return 1;if(a[x][y + 1] == '.' && a[x – 1][y + 1] == '.' && a[x – 1][y] == '.') return 1;if(a[x][y – 1] == '.' && a[x + 1][y – 1] == '.' && a[x + 1][y] == '.') return 1;if(a[x][y + 1] == '.' && a[x + 1][y + 1] == '.' && a[x + 1][y] == '.') return 1;return 0;}int main() {scanf("%d %d", &n, &m);for(int i = 1; i <= n; i++) {scanf("%s", a[i] + 1);}queue<pair<int , int> > q;for(int i = 1; i <= n; i++) {for(int j = 1; j <= m; j++) {if(check(i, j))q.push(make_pair(i, j));}}while(!q.empty()) {int i = q.front().first;int j = q.front().second;q.pop();if(!check(i, j)) continue;a[i][j] = '.';for(int x = -2; x <= 2; x++) {for(int y = -2; y <= 2; y++) {if((x || y) && check(i + x, j + y))q.push(make_pair(i + x, j + y));}}}for(int i = 1; i <= n; i++) {printf("%s\n", a[i] + 1);}return 0;}

我的TLE代码:

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <cmath>#define LL long long using namespace std;char map[2005][2005]; char tmp[2005];int vis[2005][2005];int n, m, ans;int xx[4] = {0, 1, 0, -1};int yy[4] = {1, 0, -1, 0};int r, l, up, down;//最右最左最上最下 void dfs(int a, int b) {if(a <= 0 || a > n || b <= 0 || b > m) return;if(map[a][b] == '*') return;up = max(up, a); down = min(down, a);r = max(r, b); l = min(l, b);vis[a][b] = 1;for(int i = 0; i < 4; i++) {if(!vis[a + xx[i]][b + yy[i]])dfs(a + xx[i], b + yy[i]);}vis[a][b] = 0;}void fun() {for(int i = down; i <= up; i++) {for(int j = l; j <= r; j++) {map[i][j] = '.';}}}int main() {scanf("%d %d", &n, &m);for(int i = 1; i <= n; i++) {scanf("%s", tmp);int len = strlen(tmp);for(int j = 0; j < len; j++) {map[i][j + 1] = tmp[j];}map[i][len + 1] = '\0'; }ans = 0;for(int i = 1; i <= n; i++) {for(int j = 1; j <= m; j++) {if(!vis[i][j] && map[i][j] != '*') {r = l = j;up = down = i;dfs(i, j);fun();}}}for(int i = 1; i <= n; i++)printf("%s\n", map[i] + 1);return 0;}

我要扼住命运的咽喉。

D. Arthur and Walls (判断矩形)

相关文章:

你感兴趣的文章:

标签云: