a374中等障礙物 BFS・單調事件與連鎖閉包

APCS 2024/10:連鎖反應 (ZeroJudge o713)

按到初始炸彈的距離加入格子,並讓每個正半徑炸彈只觸發一次。

前往題目練習 →回報教學錯誤V=MN、B≤1500、R≤30;時間 O(V+B·min(V,R²)),空間 O(V+B)。局部 BFS 的半徑僅指既有炸彈,不限制初始答案。

這題在學什麼

先從初始炸彈做避開石頭的 BFS,取得每格的最短步數及由近到遠順序。初始半徑為 r 時,直接引爆的恰是距離≤r 的格子。依 BFS 順序逐格加入,遇到的新正半徑炸彈放進待處理佇列,並立刻把所有連鎖反應處理完。

處理半徑 b 的炸彈時,以它為起點另做最多 b 層的局部 BFS。經過已爆格仍然可以行走;局部拜訪標記和全域已爆標記必須分開。新波及的格子只讓總數增加一次,正半徑炸彈也只入列一次;半徑 0 的格子沒有額外延伸,不必另外搜尋。

半徑增加不會讓任何已爆格消失,所以所有反應可沿用。若在加入距離 r 的某格後達到 Q,半徑 r 可行,而上一個距離層完成後尚未達標,答案就是 r;同層尚未加入的其他格只會再增加爆炸數。每顆既有正半徑炸彈最多 30,在四鄰網格中其搜尋範圍最多 1+2b(b+1) 格,石頭只會縮小此範圍。

Step-by-step animation

示意 2×3:上排 [初始,1,0],下排 [0,0,0],Q=5

回報動畫錯誤
步驟 1 / 3

初始半徑 0

僅左上初始格爆炸,共 1 格。

初始:已爆
半徑1
0
0
0
0
半徑
0
總數
1

Reference solution

回報程式錯誤

完整程式

#include <bits/stdc++.h>
using namespace std;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int rows, cols, target;
    cin >> rows >> cols >> target;
    int width = cols + 2, size = (rows + 2) * width, origin = -1;
    vector<int> grid(size, -1);
    for (int r = 1; r <= rows; ++r) {
        for (int c = 1; c <= cols; ++c) {
            int u = r * width + c;
            cin >> grid[u];
            if (grid[u] == -2)
                origin = u;
        }
    }
    int delta[] = {1, -1, width, -width};
    vector<int> distance(size, -1), order{origin};
    distance[origin] = 0;
    for (int head = 0; head < (int)order.size(); ++head) {
        int u = order[head];
        for (int step : delta) {
            int v = u + step;
            if (grid[v] != -1 && distance[v] == -1) {
                distance[v] = distance[u] + 1;
                order.push_back(v);
            }
        }
    }
    vector<char> exploded(size);
    vector<int> stamp(size), pending;
    int count = 0, token = 0;
    for (int u : order) {
        if (exploded[u])
            continue;
        int radius = distance[u];
        exploded[u] = true;
        ++count;
        if (count >= target) {
            cout << radius << '\n';
            return 0;
        }
        pending.clear();
        if (grid[u] > 0)
            pending.push_back(u);
        for (int head = 0; head < (int)pending.size(); ++head) {
            int bomb = pending[head];
            ++token;
            vector<int> local{bomb};
            stamp[bomb] = token;
            int begin = 0;
            for (int depth = 0; depth < grid[bomb]; ++depth) {
                int end = local.size();
                for (int j = begin; j < end; ++j) {
                    for (int step : delta) {
                        int v = local[j] + step;
                        if (grid[v] == -1 || stamp[v] == token)
                            continue;
                        stamp[v] = token;
                        local.push_back(v);
                        if (!exploded[v]) {
                            exploded[v] = true;
                            ++count;
                            if (count >= target) {
                                cout << radius << '\n';
                                return 0;
                            }
                            if (grid[v] > 0)
                                pending.push_back(v);
                        }
                    }
                }
                begin = end;
            }
        }
    }
}

先試著自己完成,再切換語言比對解法。參考程式可複製到題目編輯器。

更多