【題解】ZeroJudge c231: 踩地雷

【題目敘述】https://zerojudge.tw/ShowProblem?problemid=c231

#include <bits/stdc++.h>
using namespace std;

int n, m, k, a, b, ans;
set <int> st[10005];
queue <pair<int, int> > q;

int main(){
    cin >> n >> m >> k;
    for (int i = 0; i < k; i++){    
        cin >> a >> b;
        st[a].insert(b);
    }
    for (int it = 1; it <= n; it++){
        while (!st[it].empty()){
            ans++;
            q.push({it, *(st[it].begin())});
            st[it].erase(st[it].begin());
            while (!q.empty()){
                int x = q.front().first, y = q.front().second;
                q.pop();
                for (int i = max(1, x-2); i <= min(n, x+2); i++){
                    for (int j = max(1, y-2); j <= min(m, y+2); j++){
                        if (!st[i].count(j)) continue;
                        st[i].erase(j);
                        q.push({i, j});
                    }
                }
            }
        }
    }
    cout << ans << "\n";
}
分享本文 Share with friends