Notice Period

Capture the Enclosed Outposts

medium

In a territory-control game, the board is a grid of "X" (your walls) and "O" (enemy outposts). At the end of a round, every group of outposts that is fully sealed off — i.e. cannot reach the edge of the board through adjacent outposts — is captured and converted to "X". Outpost groups touching the border survive.

Given the grid board, return the board after all captures. Adjacency is horizontal and vertical only.

Example 1
in board = [["X","X","X"],["X","O","X"],["X","X","X"]]
out [["X","X","X"],["X","X","X"],["X","X","X"]]
The lone central outpost is sealed and captured.
Example 2
in board = [["O","X"],["X","X"]]
out [["O","X"],["X","X"]]
The outpost touches the border, so it survives.
Constraints
  • · 1 <= rows, cols <= 12
  • · board[r][c] is "X" or "O"
similar problem on LeetCode ↗
solution.pyloading python…
test results
Hit ▶ Run to test your code against the visible cases.