CF 525D (Arthur and Wils-Greedy 2*2 squares)

Source: Internet
Author: User

CF 525D (Arthur and Wils-Greedy 2*2 squares)

 

D. Arthur and walltime 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 rectangleN? ×?MConsisting 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 wallyou need to remove in order to achieve this goal. after removing a wall from a square it becomes a free square. while removing the Wils it is possible that some rooms unite into a single one.

Input

The first line of the input contains two integersN,?M(1? ≤?N,?M? ≤? 2000) denoting the size of the Arthur apartments.

FollowingNLines each containMSymbols-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 Wils and also this cell is contained in some of the rooms.

Output

OutputNRows each consistingMSymbols that show how the Arthur apartment plan shoshould look like after deleting the minimum number of Wils in order to make each room (maximum connected area free from Wils) 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
....................


 

 

Greedy. If a '*' must be dug out, there must be a 2*2 square, with only one '*'

Therefore, use bfs to traverse the '*' to be dug. Pay attention to the boundary.

 

 

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


 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.