Skip to content

2257. Count Unguarded Cells in the Grid #859

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We need to mark the cells that are guarded by at least one guard. The guards can see in four cardinal directions (north, south, east, and west), but their vision is blocked by walls. We can simulate this process and count the number of cells that are unguarded.

Approach:

  1. Create a grid: We can represent the grid as a 2D array where each cell can either be a wall, a guard, or empty.
  2. Mark guarded cells: For each guard, iterate in the four directions (north, south, east, west) and mark all the cells it can see, stopping when we encounter a wall or another guard.
  3. Count unguarded cells: After marking the guarded cells, count how many cells are not guarded and are not walls.

Steps:

  1. Grid Init…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@kovatz
Comment options

kovatz Nov 21, 2024
Collaborator

@mah-shamim
Comment options

mah-shamim Nov 21, 2024
Maintainer Author

Answer selected by kovatz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants