Skip to contentKEEP THE CRAFTTHE SANCTUARY

Surrounded Regions

Flip every region of 'O' cells that cannot reach the edge of the board.

The problem

Implement Solution.solve(board). The board is a grid of 'X' and 'O' cells. A region is a group of 'O' cells joined up, down, left or right (diagonals do not count). A region is captured when none of its cells can reach the edge of the board through other 'O' cells: turn every cell of a captured region into 'X'. Regions that touch the edge stay as they are. Change board in place and also return it. The board has 1 to 200 rows and 1 to 200 columns. If the board is empty, return it unchanged.

Starter code

class Solution:
    def solve(self, board):
        pass

Public examples

  1. board = [["X", "X", "X", "X"], ["X", "O", "O", "X"], ["X", "X", "O", "X"], ["X", "O", "X", "X"]]gives[["X", "X", "X", "X"], ["X", "X", "X", "X"], ["X", "X", "X", "X"], ["X", "O", "X", "X"]]
  2. board = [["O"]]gives[["O"]]
  3. board = [["X", "X", "X"], ["X", "O", "X"], ["X", "X", "X"]]gives[["X", "X", "X"], ["X", "X", "X"], ["X", "X", "X"]]

Solving it in the practice book needs an active paid plan.

Open it in the practice book
One thought at a time.