D. Arthur and Wils (CF 525 D search bfs), Arthur urbfs

Source: Internet
Author: User

D. Arthur and Wils (CF 525 D search bfs), Arthur urbfs

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 rectangleNLimit × limitMConsisting of squares of size 1 × small 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, Bytes,M(1 digit ≤ DigitN, Bytes,MLimit ≤ limit 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
....................


Question: Give a map of n * m, which consists of '*' and '. '. 'Change to the '*' To make all local '. 'can form a rectangle. Make sure that the number of modifications is the least and the last modified rectangle is output.

Idea: the initial idea was to search for Unicom blocks and change all '*' in the UNICOM blocks to '.'. However, the scope of the questions is large and the result times out. Then we can see that others are looking for a basic element block. The n * m rectangle is composed of these element blocks. It is found that if there is only one '*' in a 2*2 square, you must change this '*' to '.' So that bfs can search it again.

Code:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <map>#include <stack>#include <vector>#include <set>#include <queue>#pragma comment (linker,"/STACK:102400000,102400000")#define maxn 2005#define MAXN 2005#define mod 1000000009#define INF 0x3f3f3f3f#define pi acos(-1.0)#define eps 1e-6#define lson rt<<1,l,mid#define rson rt<<1|1,mid+1,r#define FRE(i,a,b)  for(i = a; i <= b; i++)#define FREE(i,a,b) for(i = a; i >= b; i--)#define FRL(i,a,b)  for(i = a; i < b; i++)#define FRLL(i,a,b) for(i = a; i > b; i--)#define mem(t, v)   memset ((t) , v, sizeof(t))#define sf(n)       scanf("%d", &n)#define sff(a,b)    scanf("%d %d", &a, &b)#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)#define pf          printf#define DBG         pf("Hi\n")using namespace std;typedef pair<int,int> pa;int a[maxn][maxn];char mp[maxn][maxn];int n,m;bool Isok(int x,int y){    if (x>=0&&x<n&&y>=0&&y<m)        return true;    return false;}bool num(int x,int y){    int s=a[x][y]+a[x+1][y]+a[x][y+1]+a[x+1][y+1];    if (s==3) return true;    return false;}bool change(int x,int y){    if (a[x][y]) return false;    if (num(x,y))        return true;    if (x-1>=0&&num(x-1,y))        return true;    if (y-1>=0&&num(x,y-1))        return true;    if (x-1>=0&&y-1>=0&&num(x-1,y-1))        return true;    return false;}void bfs(){    int i,j;    queue<pa>Q;    while (!Q.empty()) Q.pop();    pa st,now;    FRL(i,0,n)    {        FRL(j,0,m)        {            if (change(i,j))            {                a[i][j]=1;                Q.push(make_pair(i,j));            }        }    }    while (!Q.empty())    {        st=Q.front(); Q.pop();        FRE(i,st.first-1,st.first+1)        {            FRE(j,st.second-1,st.second+1)            {                if (Isok(i,j)&&change(i,j))                {                    a[i][j]=1;                    Q.push(make_pair(i,j));                }            }        }    }}int main(){    int i,j;    while (~sff(n,m))    {        FRL(i,0,n)            scanf("%s",mp[i]);        FRL(i,0,n)        {            FRL(j,0,m)            {                if (mp[i][j]=='*')                    a[i][j]=0;                else                    a[i][j]=1;            }        }        bfs();        FRL(i,0,n)        {            FRL(j,0,m)            {                if (a[i][j])                    pf(".");                else                    pf("*");            }            pf("\n");        }    }    return 0;}/*5 5*******.***.*.**...*******/


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.