You are given an m x n matrix board containing letters 'X' and 'O', capture regions that are surrounded:
Connect: A cell is connected to adjacent cells horizontally or vertically.
Region: To form a region connect every 'O' cell.
Surround: A region is surrounded if none of the 'O' cells in the region are on the edge of the board. Such regions are completely enclosed by 'X' cells.
To capture a surrounded region, replace all 'O's with 'X's in-place within the original board. You do not need to return anything.
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"]]Explanation: Notice that an 'O' should not be flipped if it is on the border of the board.
from collections import dequeclass Solution: # Time: O(m * n) # Space: O(m * n) for the border-connected queue in the worst case def solve(self, board: list[list[str]]) -> None: if not board or not board[0]: return m, n = len(board), len(board[0]) queue: deque[tuple[int, int]] = deque() def enqueue(i: int, j: int) -> None: if board[i][j] == "O": board[i][j] = "#" queue.append((i, j)) # Seed from all border cells for i in range(m): enqueue(i, 0) enqueue(i, n - 1) for j in range(n): enqueue(0, j) enqueue(m - 1, j) # BFS: mark every 'O' reachable from a border (cannot be captured) while queue: i, j = queue.popleft() for di, dj in ((1, 0), (-1, 0), (0, 1), (0, -1)): ni, nj = i + di, j + dj if 0 <= ni < m and 0 <= nj < n and board[ni][nj] == "O": board[ni][nj] = "#" queue.append((ni, nj)) # '#' = safe border-connected 'O'; everything else enclosed gets captured for i in range(m): for j in range(n): board[i][j] = "O" if board[i][j] == "#" else "X"