You are given two integers m
and n
representing a 0-indexed m x n
grid. You are also given two 2D integer arrays guards
and walls
where guards[i] = [rowi, coli]
and walls[j] = [rowj, colj]
represent the positions of the ith
guard and jth
wall respectively.
A guard can see every cell in the four cardinal directions (north, east, south, or west) starting from their position unless obstructed by a wall or another guard. A cell is guarded if there is at least one guard that can see it.
Return the number of unoccupied cells that are not guarded.
Example 1:
Input: m = 4, n = 6, guards = [[0,0],[1,1],[2,3]], walls = [[0,1],[2,2],[1,4]] Output: 7 Explanation: The guarded and unguarded cells are shown in red and green respectively in the above diagram. There are a total of 7 unguarded cells, so we return 7.
Example 2:
Input: m = 3, n = 3, guards = [[1,1]], walls = [[0,1],[1,0],[2,1],[1,2]] Output: 4 Explanation: The unguarded cells are shown in green in the above diagram. There are a total of 4 unguarded cells, so we return 4.
Constraints:
1 <= m, n <= 105
2 <= m * n <= 105
1 <= guards.length, walls.length <= 5 * 104
2 <= guards.length + walls.length <= m * n
guards[i].length == walls[j].length == 2
0 <= rowi, rowj < m
0 <= coli, colj < n
- All the positions in
guards
andwalls
are unique.
class Solution:
def countUnguarded(
self, m: int, n: int, guards: List[List[int]], walls: List[List[int]]
) -> int:
g = [[None] * n for _ in range(m)]
for r, c in guards:
g[r][c] = 'g'
for r, c in walls:
g[r][c] = 'w'
for i, j in guards:
for a, b in [[0, -1], [0, 1], [1, 0], [-1, 0]]:
x, y = i, j
while (
0 <= x + a < m
and 0 <= y + b < n
and g[x + a][y + b] != 'w'
and g[x + a][y + b] != 'g'
):
x, y = x + a, y + b
g[x][y] = 'v'
return sum(not v for row in g for v in row)
class Solution {
public int countUnguarded(int m, int n, int[][] guards, int[][] walls) {
char[][] g = new char[m][n];
for (int[] e : guards) {
int r = e[0], c = e[1];
g[r][c] = 'g';
}
for (int[] e : walls) {
int r = e[0], c = e[1];
g[r][c] = 'w';
}
int[][] dirs = new int[][] {{0, -1}, {0, 1}, {1, 0}, {-1, 0}};
for (int[] p : guards) {
for (int[] dir : dirs) {
int a = dir[0], b = dir[1];
int x = p[0], y = p[1];
while (x + a >= 0 && x + a < m && y + b >= 0 && y + b < n && g[x + a][y + b] != 'w'
&& g[x + a][y + b] != 'g') {
x += a;
y += b;
g[x][y] = 'v';
}
}
}
int ans = 0;
for (char[] row : g) {
for (char v : row) {
if (v == 0) {
++ans;
}
}
}
return ans;
}
}
class Solution {
public:
int countUnguarded(int m, int n, vector<vector<int>>& guards, vector<vector<int>>& walls) {
vector<vector<char>> g(m, vector<char>(n));
for (auto& e : guards) g[e[0]][e[1]] = 'g';
for (auto& e : walls) g[e[0]][e[1]] = 'w';
vector<vector<int>> dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
for (auto& p : guards) {
for (auto& dir : dirs) {
int a = dir[0], b = dir[1];
int x = p[0], y = p[1];
while (x + a >= 0 && x + a < m && y + b >= 0 && y + b < n && g[x + a][y + b] != 'w' && g[x + a][y + b] != 'g') {
x += a;
y += b;
g[x][y] = 'v';
}
}
}
int ans = 0;
for (auto& row : g)
for (auto& v : row)
ans += v == 0;
return ans;
}
};
func countUnguarded(m int, n int, guards [][]int, walls [][]int) int {
g := make([][]int, m)
for i := range g {
g[i] = make([]int, n)
}
for _, e := range guards {
g[e[0]][e[1]] = 1
}
for _, e := range walls {
g[e[0]][e[1]] = 2
}
dirs := [][]int{{0, -1}, {0, 1}, {1, 0}, {-1, 0}}
for _, p := range guards {
for _, dir := range dirs {
a, b := dir[0], dir[1]
x, y := p[0], p[1]
for x+a >= 0 && x+a < m && y+b >= 0 && y+b < n && g[x+a][y+b] != 1 && g[x+a][y+b] != 2 {
x, y = x+a, y+b
g[x][y] = 3
}
}
}
ans := 0
for _, row := range g {
for _, v := range row {
if v == 0 {
ans++
}
}
}
return ans
}