Count Unguarded Cells in the Grid LeetCode solution in Python
1class Solution:2def countUnguarded(3self, m: int, n: int, guards: List[List[int]], walls: List[List[int]]4) -> int:5dp = [[0] * n for _ in range(m)]6for x, y in guards + walls:7dp[x][y] = 189directions = [(0, 1), (1, 0), (-1, 0), (0, -1)]1011for x, y in guards:12for dx, dy in directions:13curr_x = x14curr_y = y1516while (170 <= curr_x + dx < m18and 0 <= curr_y + dy < n19and dp[curr_x + dx][curr_y + dy] != 120):21curr_x += dx22curr_y += dy23dp[curr_x][curr_y] = 22425return sum(1 for i in range(m) for j in range(n) if dp[i][j] == 0)