Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions 2257. Count Unguarded Cells in the Grid
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
char grid[100000];
class Solution {
public:
int m, n, comp;
int d[5] = {0, 1, 0, -1, 0};
inline int idx(int r, int c) { return r * n + c; }
inline void cross(int r, int c) {
for (int a = 0; a < 4; a++) {
int di = d[a], dj = d[a + 1];
for (int i = r + di, j = c + dj;; i += di, j += dj) {
int pos = idx(i, j);
if (i < 0 || i >= m || j < 0 || j >= n || grid[pos] == 'X')
break;
comp -= (grid[pos] == ' ');
grid[pos] = 'V';
}
}
}

int countUnguarded(int m, int n, vector<vector<int>>& guards,
vector<vector<int>>& walls) {
this->m = m, this->n = n;
comp = m * n;
memset(grid, ' ', m * n);

for (auto& ij : walls) {
grid[idx(ij[0], ij[1])] = 'X';
comp--;
}

for (auto& ij : guards) {
grid[idx(ij[0], ij[1])] = 'X';
comp--;
}

for (auto& ij : guards) {
cross(ij[0], ij[1]);
}
return comp;
}
};
Loading