天天看点

Codeforces Round #297 (Div. 2) 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的小方格,方格恰好包括一个星号,

然后把星号变点,再找出因此而满足上述条件的星号,

重复上述条件……

#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<climits>
#include<list>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;
queue<int>qq;
char pic[2010][2010];
int n,m;
bool ok(int i,int j)
{
	if(pic[i][j]=='.')
		return 0;
	if(i-1>=0&&j+1<m&&pic[i-1][j]=='.'&&pic[i-1][j+1]=='.'&&pic[i][j+1]=='.')
		return 1;
	if(i-1>=0&&j-1>=0&&pic[i-1][j]=='.'&&pic[i-1][j-1]=='.'&&pic[i][j-1]=='.')
		return 1;
	if(i+1<n&&j+1<m&&pic[i+1][j]=='.'&&pic[i+1][j+1]=='.'&&pic[i][j+1]=='.')
		return 1;
	if(i+1<n&&j-1>=0&&pic[i+1][j]=='.'&&pic[i+1][j-1]=='.'&&pic[i][j-1]=='.')
		return 1;
	return 0;
}
bool vis[2010][2010];
int tox[8]={-1,-1,0,1,1,1,0,-1};
int toy[8]={0,1,1,1,0,-1,-1,-1};
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=0;i<n;i++)
		scanf("%s",pic[i]);
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
			if(ok(i,j))
			{
				qq.push(i);
				qq.push(j);
				vis[i][j]=1;
			}
	while(qq.size())
	{
		int x=qq.front();qq.pop();
		int y=qq.front();qq.pop();
		pic[x][y]='.';
		for(int i=0;i<8;i++)
		{
			int nx=x+tox[i],ny=y+toy[i];
			if(!vis[nx][ny]&&ok(nx,ny))
			{
				qq.push(nx);
				qq.push(ny);
				vis[nx][ny]=1;
			}
		}
	}
	for(int i=0;i<n;i++)
		puts(pic[i]);
}