Surrounded Regions
Given an m x n board of 'X' and 'O', capture all regions that are 4-directionally surrounded by 'X' by flipping every 'O' in such a region to 'X'. An 'O' region is safe only if it touches the board's border.
Open official problem prompt ↗Flip only the O regions that are completely enclosed by X, leaving border-connected O regions untouched.
Think of the border as the only exit from a flooded building; any water (O) that can trace a path to an exit stays, and any water trapped in an inner room gets sealed off (turned to X).
- Input
- board = [["X","X","X","X"],["X","O","O","X"],["X","X","O","X"],["X","O","X","X"]]
- Output
- [["X","X","X","X"],["X","X","X","X"],["X","X","X","X"],["X","O","X","X"]]
- Why
- The inner O region is fully surrounded and captured; the bottom-row O at (3,1) touches the border so it survives.
m == board.lengthn == board[i].length1 <= m, n <= 200board[i][j] is 'X' or 'O'