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

代码

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 #define mem(a) memset(a,0,sizeof(a))
 5 #define mp(x,y) make_pair(x,y)
 6 const int INF = 0x3f3f3f3f;
 7 const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
 8 inline ll read(){
 9     ll x=0,f=1;char ch=getchar();
10     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
11     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
12     return x*f;
13 }
14 //
15 const int maxn = 2e3+10;
16 
17 char mp[maxn][maxn];
18 int dx[8] = {0,0,1,-1,1,1,-1,-1};
19 int dy[8] = {1,-1,0,0,1,-1,1,-1};
20 int n,m;
21 
22 void check(int x,int y){
23     int cnt = 0;
24     for(int i=0; i<2; i++)
25         for(int j=0; j<2; j++){
26             if(mp[x+i][y+j] == '.')
27                 cnt++;
28         }
29     if(cnt == 3){
30         for(int i=0; i<2; i++)
31             for(int j=0; j<2; j++)
32                 mp[x+i][y+j] = '.';
33         for(int i=0; i<8; i++){
34             int tx=x+dx[i],ty=y+dy[i];
35             if(tx<1 || tx>n || ty<1 || ty>m) continue;
36             check(tx,ty);
37         }
38     }
39 }
40 
41 int main(){
42     n=read(),m=read();
43     for(int i=1; i<=n; i++)
44         for(int j=1; j<=m; j++)
45             scanf(" %c",&mp[i][j]); 
46 
47     for(int i=1; i<=n; i++)
48         for(int j=1; j<=m; j++)
49             check(i,j);
50 
51     for(int i=1; i<=n; i++){
52         for(int j=1; j<=m; j++)
53             printf("%c",mp[i][j]);
54         puts("");
55     }
56 
57 
58     return 0;
59 }

 

转载于:https://www.cnblogs.com/yxg123123/p/6827695.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值