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

本文解析了CodeForces上的一道题目,该题要求在一块n*m的田地中,通过移除最少数量的'*'符号,使得所有'.'符号构成的连通块形成矩形。文中提供了一种解决方案,并附带了实现该方案的C++代码。

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

题目链接:

http://codeforces.com/contest/525/problem/D

题意

给你一个n*m的田地,有一些*的地方是可以移除变成"."的,然后问你移除最少的"*",使的每一个"."的联通块都是矩形

题解:

2*2 的矩形中,如果有一个 '*' 与三个 '.' ,那么这个 '*' 就一定要变成 ‘.' ,然后bfs

代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define mp(x,y) make_pair(x,y)
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
inline ll read(){
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
//////////////////////////////////////////////////////////////////////////
const int maxn = 2e3+10;

char mp[maxn][maxn];
int dx[8] = {0,0,1,-1,1,1,-1,-1};
int dy[8] = {1,-1,0,0,1,-1,1,-1};
int n,m;

void check(int x,int y){
    int cnt = 0;
    for(int i=0; i<2; i++)
        for(int j=0; j<2; j++){
            if(mp[x+i][y+j] == '.')
                cnt++;
        }
    if(cnt == 3){
        for(int i=0; i<2; i++)
            for(int j=0; j<2; j++)
                mp[x+i][y+j] = '.';
        for(int i=0; i<8; i++){
            int tx=x+dx[i],ty=y+dy[i];
            if(tx<1 || tx>n || ty<1 || ty>m) continue;
            check(tx,ty);
        }
    }
}

int main(){
    n=read(),m=read();
    for(int i=1; i<=n; i++)
        for(int j=1; j<=m; j++)
            scanf(" %c",&mp[i][j]); 

    for(int i=1; i<=n; i++)
        for(int j=1; j<=m; j++)
            check(i,j);

    for(int i=1; i<=n; i++){
        for(int j=1; j<=m; j++)
            printf("%c",mp[i][j]);
        puts("");
    }


    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值