Loading problem...
You are given a two - dimensional grid of size m × n called board, where each cell contains either the character 'X' (representing a wall) or 'O' (representing open territory). Your objective is to capture all enclosed open territories by converting them to walls.
A group of connected open territory cells forms a zone. Two cells are considered connected if they share an edge horizontally or vertically (diagonal connections do not count). A zone is considered enclosed if it is completely surrounded by walls on all sides—meaning none of the cells in the zone touch any edge (boundary) of the board.
To capture an enclosed zone, you must replace all 'O' cells in that zone with 'X' cells in-place directly within the original board.
Key Definitions:
'O' cells.'O' cells in an enclosed zone with 'X' cells.Note: Zones that have at least one cell touching the board's boundary cannot be captured and must remain unchanged.
board = [["X","X","X","X"],["X","O","O","X"],["X","X","O","X"],["X","O","X","X"]][["X","X","X","X"],["X","X","X","X"],["X","X","X","X"],["X","O","X","X"]]The zone containing the three 'O' cells at positions (1,1), (1,2), and (2,2) is completely enclosed by 'X' walls and doesn't touch any edge. This zone is captured by converting all its cells to 'X'. However, the 'O' at position (3,1) is on the bottom row boundary, so it forms a zone that cannot be enclosed—it remains as 'O'.
board = [["X"]][["X"]]A single cell board with a wall. No open territory exists, so no changes are needed.
board = [["X","X","X"],["X","O","X"],["X","X","X"]][["X","X","X"],["X","X","X"],["X","X","X"]]The single 'O' cell at position (1,1) is completely enclosed by 'X' walls on all four sides and does not touch any boundary edge. This cell is captured and converted to 'X'.
Constraints