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

本文通过BFS算法解决了一个关于矩阵中最小替换问题的挑战,目标是通过替换'*'为'.',使得'.'形成的区域尽可能保持矩形形状。通过分析矩阵中的'.',我们采用BFS从这些点开始扩散,探索周围可能需要替换的'*',以形成更大的矩形区域。

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

 

题目传送门

 1 /*
 2     题意:问最少替换'*'为'.',使得'.'连通的都是矩形
 3     BFS:搜索想法很奇妙,先把'.'的入队,然后对于每个'.'八个方向寻找
 4         在2*2的方格里,若只有一个是'*',那么它一定要被替换掉
 5 */
 6 #include <cstdio>
 7 #include <iostream>
 8 #include <algorithm>
 9 #include <cstring>
10 #include <queue>
11 using namespace std;
12 
13 const int MAXN = 2e3 + 10;
14 const int INF = 0x3f3f3f3f;
15 int n, m;
16 int dx[4][3] = {{1,0,1},{0,-1,-1},{-1,-1,0},{1,1,0}};
17 int dy[4][3] = {{0,1,1},{1,0,1},{0,-1,-1},{0,-1,-1}};
18 char s[MAXN][MAXN];
19 
20 bool ok(int x, int y)
21 {
22     if (x < 0 || x >= n)    return false;
23     if (y < 0 || y >= m)    return false;
24 
25     return true;
26 }
27 
28 void BFS(void)
29 {
30     queue<pair<int, int> > Q;
31     for (int i=0; i<n; ++i)
32     {
33         for (int j=0; j<m; ++j)
34         {
35             if (s[i][j] == '.')
36             {
37                 Q.push (make_pair (i, j));
38             }
39         }
40     }
41 
42     while (!Q.empty ())
43     {
44         int x = Q.front ().first;    int y = Q.front ().second;
45         Q.pop ();
46         for (int i=0; i<4; ++i)
47         {
48             int cnt = 0;    int px, py;    bool flag = true;
49             for (int j=0; j<3; ++j)
50             {
51                 int tx = x + dx[i][j];    int ty = y + dy[i][j];
52                 if (ok (tx, ty))
53                 {
54                     if (s[tx][ty] == '*')
55                     {
56                         cnt++;    px = tx;    py = ty;
57                     }
58                 }
59                 else    flag = false;
60             }
61             if (flag && cnt == 1)
62             {
63                 s[px][py] = '.';    Q.push (make_pair (px, py));
64             }
65         }
66     }
67 }
68 
69 int main(void)        //Codeforces Round #297 (Div. 2) D. Arthur and Walls
70 {
71     while (scanf ("%d%d", &n, &m) == 2)
72     {
73         for (int i=0; i<n; ++i)    scanf ("%s", s[i]);
74         BFS ();
75         for (int i=0; i<n; ++i)    printf ("%s\n", s[i]);
76     }
77 
78     return 0;
79 }
80 
81 
82 /*
83 5 5
84 .*.*.
85 *****
86 .*.*.
87 *****
88 .*.*.
89 6 7
90 ***.*.*
91 ..*.*.*
92 *.*.*.*
93 *.*.*.*
94 ..*...*
95 *******
96 */

 

转载于:https://www.cnblogs.com/Running-Time/p/4534492.html

### 关于Codeforces Round 971 (Div. 4)比赛题目及解析 #### A-G1题解概述 对于Codeforces Round 971 (Div. 4),该轮次的比赛涵盖了多个难度级别的编程挑战,旨在测试参赛者的算法思维能力和编码技巧。其中,A至G1的题目设计覆盖了基础数据结构操作、贪心策略应用以及动态规划等多个方面。 #### G2. Yunli’s Subarray Queries (Hard Version) 具体到G2这道难题目,其背景设定在一个由n个节点构成的无向图环境中,此图恰好拥有\( n-1 \)条带权重的边,每一边连接着两个连续编号的顶点,并且初始状态下每个顶点都放置了一个与其自身编号相匹配的小球[^1]。问题的核心在于计算一种最优方案下的总成本——即通过调整各小球的位置让它们不再位于原本对应的顶点之上;每一次位置交换都需要支付相应路径上的代价(即边的权重),而目标就是找到能够满足上述条件的同时使总的迁移费用达到最低的方法。 针对此类涉及最短路径求解的问题,通常采用广度优先搜索(BFS)或迪杰斯特拉(Dijkstra&#39;s Algorithm)等经典图论算法来进行处理。然而,在本题特殊条件下,则更倾向于利用差分数组配合线段树/树状数组来高效解决区间更新与查询的需求,从而快速响应多次修改后的即时状态变化并给出正确解答[^2]。 ```cpp #include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int a[N], b[N]; long long sum[N << 2]; void push_up(int rt){ sum[rt] = min(sum[rt<<1],sum[rt<<1|1]); } // build, update and query functions omitted for brevity... int main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n,q,x,y,z; cin >> n >> q; // initialization code here... } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值