程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> CF 525D(Arthur and Walls

CF 525D(Arthur and Walls

編輯:關於C++

 

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
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
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=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 > 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 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;
}


 

 

 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved