> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Count Unguarded Cells in the Grid

> Tested Python solution for LeetCode 2257 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 2257, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Matrix](/catalog/topics/matrix), [Simulation](/catalog/topics/simulation). [View on LeetCode](https://leetcode.com/problems/count-unguarded-cells-in-the-grid/description/).

Generate this problem as a practice environment: tested reference solution, 20 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 2257   # by problem number
lcpy gen -s count_unguarded_cells_in_the_grid   # by problem name
```

## Problem

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**.*

### Examples

![Example 1](https://assets.leetcode.com/uploads/2022/03/10/example1drawio2.png)

```
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](https://assets.leetcode.com/uploads/2022/03/10/example2drawio.png)

```
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 <= 10^5`
* `2 <= m * n <= 10^5`
* `1 <= guards.length, walls.length <= 5 * 10^4`
* `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` and `walls` are **unique**.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/count_unguarded_cells_in_the_grid/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/count_unguarded_cells_in_the_grid/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(m * n); every cell is scanned a constant number of times.
    # Space: O(m * n) for the grid.
    def count_unguarded(
        self, m: int, n: int, guards: list[list[int]], walls: list[list[int]]
    ) -> int:
        grid = [[0] * n for _ in range(m)]
        for row, col in guards:
            grid[row][col] = 1
        for row, col in walls:
            grid[row][col] = 2

        for row, col in guards:
            for d_row, d_col in ((-1, 0), (1, 0), (0, -1), (0, 1)):
                next_row, next_col = row + d_row, col + d_col
                while (
                    0 <= next_row < m and 0 <= next_col < n and grid[next_row][next_col] in (0, 3)
                ):
                    grid[next_row][next_col] = 3
                    next_row += d_row
                    next_col += d_col

        return sum(row.count(0) for row in grid)
```

## Complexity

| Time | Space |
| - | - |
| O(m \* n); every cell is scanned a constant number of times. | O(m \* n) for the grid. |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.