D. Arthur and Walls (CF 525 D search BFS)

Source: Internet
Author: User

D. Arthur and Wallstime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard output

Finally It was a day when Arthur had enough money for buying an apartment. He found a great option close to the center of the city with a nice price.

Pla N of the apartment found by Arthur looks like a rectangle  ").

The apartment is a maximal connected area consisting of free squares. Squares is considered adjacent if they share a common side.

The old Arthur dream was to live in a apartment where all rooms is 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 was possible that some rooms unite to a single one.

Input

The first line of the input contains the 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's this cell is free from walls and also this cell is contained in so Me of the rooms.

Output

Output n rows each consisting of m symbols so show how the Arthur apartment plan should look like Afte R Deleting the minimum number of walls in order to make each guest (maximum connected area free from walls) is a rectangle.

If there is several possible answers, output any of them.

Sample Test (s) input
5 5.*.*.*****.*.*.*****.*.*.
Output
.*.*.*****.*.*.*****.*.*.
Input
6 7***.*.*. *.*.**.*.*.**.*.*.*.. *...********
Output
***...*.. *...*.. *...*.. *...*.. *...********
Input
4 5............***. *..
Output
....................


Test instructions: Give a map of n*m, by ' * ' and '. ' Number, and now we're going to have some '. ' Change the ' * ' to make all the local '. ' Can form a rectangle, to ensure the least number of changes, and finally output the changed rectangle.

Idea: The first idea is to search the Unicom block, the Unicom block inside the ' * ' all changed to '. ', but the scope of the topic is larger, the result has timed out. And then see someone else is looking for a block of basic elements, the n*m rectangle consists of these element blocks. Discovery: If only one in a 2*2 box is ' * ' then you have to change this ' * ' to '. ' So that the bfs can be searched 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 RSO  N 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, si Zeof (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 PA ir<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*******.***.*.**...*******/


D. Arthur and Walls (CF 525 D search BFS)

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.