top1编程
← 返回题目
题解

【Mc生存】插火把

1 条题解

  • 0
    @ 2026-10-20 19:15:28

    1. 解题思路

    **核心方法:**枚举每个光源周围5乘5的格子,按光源类型判断是否照亮,再统计未亮位置。

    2. 坑点和注意事项

    • 火把要求曼哈顿距离≤2,萤石覆盖整个5乘5;只有坐标在1到n内才写数组。int足够。
    /*
    思路:枚举每个光源周围5乘5的格子,按光源类型判断是否照亮,再统计未亮位置。
    关键规则与坑点:火把要求曼哈顿距离≤2,萤石覆盖整个5乘5;只有坐标在1到n内才写数组。int足够。
    时间复杂度:O(n²+m+k)。空间复杂度:O(n²)。
    */
    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        int n, m, k, x, y;
        // 读取题目给定数据。
        cin >> n >> m >> k;
        bool lit[101][101] = {};
        for (int t = 0; t < m + k; t++)
        {
            cin >> x >> y;
            for (int dx = -2; dx <= 2; dx++)
            {
                for (int dy = -2; dy <= 2; dy++)
                {
                    // 枚举每个光源周围5乘5的格子,按光源类型判断是否照亮,再统计未亮位置。
                    if (t >= m || abs(dx) + abs(dy) <= 2)
                    {
                        int i = x + dx, j = y + dy;
                        if (i >= 1 && i <= n && j >= 1 && j <= n)
                        {
                            lit[i][j] = 1;
                        }
                    }
                }
            }
        }
        int ans = 0;
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                ans += !lit[i][j];
            }
        }
        cout << ans << "\n";
    }
    

    3. 复杂度

    • **时间复杂度:**O(n²+m+k)。
    • **空间复杂度:**O(n²)。
    • 1