3
// store info about rows , coulms and diagonal that is illuminated4
// if (i,j) is lamp , then every cell (x,y) is illuminated if5
// x==i (row) || y==j (column) || x+y == i+j (anti diagonal) || x-y == i-j7
vector<int> gridIllumination(int n, vector<vector<int>> &lamps, vector<vector<int>> &queries) {9
unordered_map<int, int> row, col, mainDiag, antiDiag;10
set<pair<int, int>> s;12
for (auto &l : lamps) {13
int x = l[0], y = l[1];14
// Even if the same lamp is listed more than once, it is turned on.15
// To prevent duplicate entries16
if (s.find({x, y}) == s.end()) {21
s.insert({x, y}); // This helps us to get the lamp in O(1)25
for (auto &q : queries) {26
// For every query {x,y} do 2 things :27
// 1. Check whether grid[x][y] is illuminated or not, accordingly set28
// answer either to '1' OR '0'29
// 2. if grid is illuminated, turn off the lamps at {x,y} and 8 adjacent32
int x = q[0], y = q[1];33
if (row[x] > 0 or col[y] > 0 or antiDiag[x + y] > 0 or mainDiag[x - y] > 0) {37
// Turn off the 9 lamps = 1 center and 8 adjacent lamps38
for (int i = -1; i <= 1; i++) {39
for (int j = -1; j <= 1; j++) {42
// NxN board, check boundaries condition43
if (nx >= 0 and nx < n and ny >= 0 and ny < n and s.find({nx, ny}) != s.end()) {52
} else // No illumination