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的子矩形里面如果有一个字符是 * 说明我们需要修改字符 *。那么直接扫描一遍,因为修改后会影响到周围8个格子,那么直接DFS。
不过在dfs过程中,找到包含当前点的四个2*2矩形也是有技巧的,在这里记录一下;
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define inf 0x3f3f3f3f
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rep1(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
const int N= 2020;
char mapp[N][N];
int r,c;
int dx[5]={0,1,0,-1,0}; //前一个和后一个dx,dy坐标呈90度
int dy[5]={1,0,-1,0,1};
int f(int a,int b)
{
return a? a:b;
}
void dfs(int x,int y)
{
if(mapp[x][y]!='*') return;
for(int i=0;i<4;i++)
{
int x1=x+dx[i];
int y1=y+dy[i];
int x2=x+dx[i+1];
int y2=y+dy[i+1];
int x3=x+f(dx[i],dx[i+1]);//对角线点的坐标
int y3=y+f(dy[i],dy[i+1]);
if(mapp[x1][y1]=='.'&&mapp[x2][y2]=='.'&&mapp[x3][y3]=='.')
{
mapp[x][y]='.';
for(int nx=x-1;nx<=x+1;nx++) //开始把'.'感染开来
for(int ny=y-1;ny<=y+1;ny++)
dfs(nx,ny);
return ;
}
}
}
int main()
{
cin>>r>>c;
for(int i=1;i<=r;i++)
cin>>mapp[i]+1;
for(int i=1;i<=r;i++)
for(int j=1;j<=c;j++)
dfs(i,j);
for(int i=1;i<=r;i++)
cout<<mapp[i]+1<<endl;
return 0;
}