Codeforces Round #297 (Div. 2) D Arthur and Walls(bfs)

本博客介绍了一种算法,用于计算在公寓平面图中移除最少数量的墙壁,以确保所有房间均为矩形。算法通过识别并处理特定形状的墙壁,最终输出修改后的平面图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
.....
.....
.....
.....


题意: .表示空地,要去掉最少的*使得.围成的每一个块都是矩形


思路:对于每一个*,什么情况下是需要去掉的呢? 将 L,每次旋转90度的形状是需要去掉的,那么每次去掉一个 *,检查是否还需要去掉*





#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>


#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define eps 1e-8
typedef __int64 ll;

#define fre(i,a,b)  for(i = a; i <b; i++)
#define free(i,b,a) for(i = b; i >= a;i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define ssf(n)      scanf("%s", n)
#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 bug         pf("Hi\n")

using namespace std;

#define INF 0x3f3f3f3f
#define N 2005

int n,m;
char a[N][N];
typedef pair<int,int>p;


bool judge(int x,int y)
{
     if(x<0||x>=n||y<0||y>=m||a[x][y]=='.') return false;
     int i=x,j=y;

     if(a[i][j-1]=='.'&&a[i+1][j-1]=='.'&&a[i+1][j]=='.') return true;

     if(a[i-1][j]=='.'&&a[i-1][j+1]=='.'&&a[i][j+1]=='.') return true;

     if(a[i][j-1]=='.'&&a[i-1][j]=='.'&&a[i-1][j-1]=='.') return true;

     if(a[i][j+1]=='.'&&a[i+1][j]=='.'&&a[i+1][j+1]=='.') return true;

     return false;
}

void solve()
{
    queue<p>q;
    int i,j;
    fre(i,0,n)
     fre(j,0,m)
      if(judge(i,j))
        q.push(p(i,j));

    p cur;

    while(!q.empty())
    {
        cur=q.front();
        q.pop();
        int x=cur.first;
        int y =cur.second;
        if(!judge(x,y)) continue;
        a[x][y]='.' ;

        fre(i,-1,2)
          fre(j,-1,2)
          {
              if(judge(i+x,y+j))
                q.push(p(i+x,y+j));
          }
    }
    fre(i,0,n)
      pf("%s\n",a[i]);

}
int main()
{
    int i,j;
    while(~sff(n,m))
    {
        fre(i,0,n)
         ssf(a[i]);

        solve();
    }
  return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值