Codeforces Round #297 (Div. 2 ),
D. Arthur and Wallstime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard 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 rectangleNLimit × limitMConsisting of squares of size 1 × small 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 wallyou need to remove in order to achieve this goal. after removing a wall from a square it becomes a free square. while removing the Wils it is possible that some rooms unite into a single one.
Input
The first line of the input contains two integersN, Bytes,M(1 digit ≤ DigitN, Bytes,MLimit ≤ limit 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 Wils and also this cell is contained in some of the rooms.
Output
OutputNRows each consistingMSymbols that show how the Arthur apartment plan shoshould look like after deleting the minimum number of Wils in order to make each room (maximum connected area free from Wils) 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
....................
Idea: I first thought of dfs. Each time I found the top, bottom, left, and rightmost of a path, I assigned all values to 'In this rectangle '. ', later I found it was too slow, and all kinds of backtracking, TLE, would be very boring, and very boring data. Then I read some people's practices and found it very interesting, it can be composed of 2*2 rectangles.
....
. * Or *.
Or in a similar case, use the queue to simulate
AC code:
#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;}
My TLE code:
# Include <cstdio> # include <cstring> # include <iostream> # include <algorithm> # include <cmath> # define LL 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); do Wn = 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", & 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 ;}