Loading

Leetcode 1001. 网格照明(map)

在大小为 n x n 的网格 grid 上,每个单元格都有一盏灯,最初灯都处于 关闭 状态。

给你一个由灯的位置组成的二维数组 lamps ,其中 lamps[i] = [rowi, coli] 表示 打开 位于 grid[rowi][coli] 的灯。即便同一盏灯可能在 lamps 中多次列出,不会影响这盏灯处于 打开 状态。

当一盏灯处于打开状态,它将会照亮 自身所在单元格 以及同一 行 、同一 列 和两条 对角线 上的 所有其他单元格 。

另给你一个二维数组 queries ,其中 queries[j] = [rowj, colj] 。对于第 j 个查询,如果单元格 [rowj, colj] 是被照亮的,则查询结果为 1 ,否则为 0 。在第 j 次查询之后 [按照查询的顺序] ,关闭 位于单元格 grid[rowj][colj] 上及相邻 8 个方向上(与单元格 grid[rowi][coli] 共享角或边)的任何灯。

返回一个整数数组 ans 作为答案, ans[j] 应等于第 j 次查询 queries[j] 的结果,1 表示照亮,0 表示未照亮。

示例 1:

输入:n = 5, lamps = [[0,0],[4,4]], queries = [[1,1],[1,0]]
输出:[1,0]
解释:最初所有灯都是关闭的。在执行查询之前,打开位于 [0, 0] 和 [4, 4] 的灯。第 0 次查询检查 grid[1][1] 是否被照亮(蓝色方框)。该单元格被照亮,所以 ans[0] = 1 。然后,关闭红色方框中的所有灯。

第 1 次查询检查 grid[1][0] 是否被照亮(蓝色方框)。该单元格没有被照亮,所以 ans[1] = 0 。然后,关闭红色矩形中的所有灯。

示例 2:

输入:n = 5, lamps = [[0,0],[4,4]], queries = [[1,1],[1,1]]
输出:[1,1]
示例 3:

输入:n = 5, lamps = [[0,0],[0,4]], queries = [[0,4],[0,1],[1,4]]
输出:[1,1,0]

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/grid-illumination
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

注意到数据范围到了1e9,直接用map模拟。一共开了5个map,分别记录:[x, y]这个位置有没有灯,x这一行/y这一列被覆盖多少次,[x - y]/[x + y - n]这两个斜行被覆盖多少次。每次修改直接暴力维护即可。注意一个位置可能开灯多次,需要判断,否则会造成覆盖次数的额外增加。

class Solution {
public:
    map<int, map<int, int> > mp;
    map<int, int> r, c;
    map<int, int> a, b;
    int len = 0, lenq = 0;
    int d[9][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {-1, -1}, {1, -1}, {-1, 1}, {0, 0}};
    vector<int> gridIllumination(int n, vector<vector<int>>& lamps, vector<vector<int>>& queries) {
        len = lamps.size();
        lenq = queries.size();
        vector<int> ans;
        for(int i = 0; i < len; i++) {
            int x = lamps[i][0], y = lamps[i][1];
            if(mp.find(x) != mp.end() && mp[x].find(y) != mp[x].end()) continue;
            mp[x][y] = 1;
            if(r.find(x) == r.end()) r[x] = 1;
            else r[x]++;
            if(c.find(y) == c.end()) c[y] = 1;
            else c[y]++;
            if(a.find(x - y) == a.end()) a[x - y] = 1;
            else a[x - y]++;
            if(b.find(x + y - n) == b.end()) b[x + y - n] = 1;
            else b[x + y - n]++;
        }
        for(int i = 0; i < lenq; i++) {
            int qx = queries[i][0], qy = queries[i][1];
            if(r.find(qx) != r.end() && r[qx] > 0 || c.find(qy) != c.end() && c[qy] > 0) {
                ans.push_back(1);
            } else if(a.find(qx - qy) != a.end() && a[qx - qy] > 0 || b.find(qx + qy - n) != b.end() && b[qx + qy - n] > 0) {
                ans.push_back(1);
            } else {
                ans.push_back(0);
            }
            for(int j = 0; j < 9; j++) {
                int nx = qx + d[j][0], ny = qy + d[j][1];
                if(nx < 0 || nx >= n || ny < 0 || ny >= n) continue;
                if(mp.find(nx) != mp.end() && mp[nx].find(ny) != mp[nx].end()) {
                    mp[nx].erase(ny);
                    r[nx]--;
                    c[ny]--;
                    a[nx - ny]--;
                    b[nx + ny - n]--;
                }
            }
        }
        return ans;
    }
};
posted @ 2022-02-09 21:57  脂环  阅读(36)  评论(0编辑  收藏  举报