CF 525D(Arthur and Walls-貪心2*2方格補全),525dwalls-

來源:互聯網
上載者:User

CF 525D(Arthur and Walls-貪心2*2方格補全),525dwalls-

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 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;}





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.