您的位置:首页 > 其它

CF 525D(Arthur and Walls-贪心2*2方格补全)

2015-03-27 20:18 295 查看
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
.....
.....
.....
.....


贪心,如果一个‘*’必须挖掉,则必然存在1个2*2方格,只有它1个'*'

所以用bfs遍历待挖’*‘,注意边界

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
#include<queue>
#include<vector>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (2000+10)
typedef long long ll;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int n,m;
char s[MAXN][MAXN];
int a[MAXN][MAXN]={0};
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
int dire[3]={-1,0,1};
bool inside(int i,int j){return (1<=i&&i<=n&&1<=j&&j<=m);}
queue<pair<int,int> > q;
int sum(int i,int j)
{
return a[i][j]+a[i][j+1]+a[i+1][j]+a[i+1][j+1];
}
bool shall_destroy(int i,int j)
{
if (a[i][j]) return 0;
if (sum(i,j)==3||sum(i-1,j)==3||sum(i,j-1)==3||sum(i-1,j-1)==3) return 1;
return 0;
}
int main()
{
//	freopen("Walls.in","r",stdin);

scanf("%d%d",&n,&m);
For(i,n)
{
scanf("%s",s[i]+1);
}
For(i,n)
For(j,m) if (s[i][j]=='.') a[i][j]=1;

while(!q.empty()) q.pop();

For(i,n)
For(j,m)
if (shall_destroy(i,j))
{
a[i][j]=1;
q.push(make_pair(i,j));
}

while(!q.empty())
{
pair<int,int> now=q.front();
q.pop();
int x=now.first,y=now.second;
Fork(i,x-1,x+1)
Fork(j,y-1,y+1)
if(inside(i,j)&&shall_destroy(i,j))
{
a[i][j]=1;
q.push(make_pair(i,j));
}

}

For(i,n)
{
For(j,m)
if (a[i][j]) putchar('.');
else putchar('*');
printf("\n");
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: