codeforces#297div2_d bfs,2*2法判断矩阵里的矩形

codeforces#297div2_d bfs,2*2法判断矩阵里的矩形

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
.....
.....
.....
.....
题意:对一个矩阵,'.'为空地,'*'为墙,移开部分墙,使矩阵里的空地都是矩形,输出移开的最小个墙后的矩阵
思路:首先明确一个事实,如果矩阵里面的空地都是矩形,则对每一个2*2的子矩阵,一定不会只有一个墙和三个空地,只能是其它情况。
错解:直接遍历子矩阵
正解:bfs,让空地入队,检查队列里每个元素的2*2,若发现有‘1’+‘3’,将‘1’改为空地并让其入队。另外初始化时将整个矩阵初始为墙,避免边界处理。
错解:
/** 
错解

hack数据
5 5
..***
..***
..***
.....
.....
应该输出
.....
.....
.....
.....
.....
实际输出
...**
...**
.....
.....
.....
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>

using namespace std;

const int maxn=2200;
const int INF=(1<<29);
const double EPS=0.00000001;

int n,m;
char ch;
int op[maxn][maxn];

int main()
{
    cin>>n>>m;
    memset(op,0,sizeof(op));
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>ch;
            if(ch!='.') op[i][j]=1;
        }
    }
    for(int i=1;i<n;i++){
        for(int j=1;j<m;j++){
            if(op[i][j]+op[i+1][j]+op[i][j+1]+op[i+1][j+1]==1)
                op[i][j]=op[i+1][j]=op[i][j+1]=op[i+1][j+1]=0;
        }
    }
    for(int i=n;i>1;i--){
        for(int j=m;j>1;j--){
            if(op[i][j]+op[i-1][j]+op[i][j-1]+op[i-1][j-1]==1)
                op[i][j]=op[i-1][j]=op[i][j-1]=op[i-1][j-1]=0;
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            printf("%c",op[i][j]?'*':'.');
        }
        printf("\n");
    }
    return 0;
}
View Code

正解:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<queue>

using namespace std;

const int maxn=2200;
const int INF=(1<<29);
const double EPS=0.00000001;

int n,m;
char ch;
bool op[maxn][maxn];
struct Node
{
    int x,y;
};
queue<Node> q;

void check(int x,int y,int dx,int dy)
{
    if((int)op[x][y]+(int)op[x+dx][y]+(int)op[x][y+dy]+(int)op[x+dx][y+dy]==1){
        if(op[x][y]) q.push({x,y});
        if(op[x+dx][y]) q.push({x+dx,y});
        if(op[x][y+dy]) q.push({x,y+dy});
        if(op[x+dx][y+dy]) q.push({x+dx,y+dy});
        op[x][y]=op[x+dx][y]=op[x][y+dy]=op[x+dx][y+dy]=0;
    }
}

int main()
{
    cin>>n>>m;
    memset(op,1,sizeof(op));
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>ch;
            if(ch=='.'){
                op[i][j]=0;
                q.push({i,j});
            }
        }
    }
    ///bfs
    while(!q.empty()){
        Node now=q.front();q.pop();
        int x=now.x,y=now.y;
        check(x,y,1,1);
        check(x,y,-1,1);
        check(x,y,-1,-1);
        check(x,y,1,-1);
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            printf("%c",op[i][j]?'*':'.');
        }
        printf("\n");
    }
    return 0;
}
View Code

 

posted @ 2015-04-11 15:17  __560  阅读(258)  评论(0编辑  收藏  举报