<jupyter_start><jupyter_text>```Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'.A region is captured by flipping all 'O's into 'X's in that surrounded region.Example:X X X XX O O XX X O XX O X XAfter running your function, the board should be:X X X XX X X XX X X XX O X XExplanation:Surrounded regions shouldn’t be on the border, which means that any 'O' on the border of the board are not flipped to 'X'. Any 'O' that is not on the border and it is not connected to an 'O' on the border will be flipped to 'X'. Two cells are connected if they are adjacent cells connected horizontally or vertically.``` Solution 1. Using BFS<jupyter_code>from typing import List
from collections import deque
class Solution:
    paths = [[-1, 0], [1, 0], [0, -1], [0, 1]]    
    def solve(self, board: List[List[str]]) -> None:
        if not board or not board[0]:
            return []
        m = len(board)
        n = len(board[0])
        q = deque()
        visited = [[0]*n for _ in range(m)]
        for i in range(m):
            if board[i][0] == 'O':
                q.append([i, 0])
                visited[i][0] = 1
            if board[i][n-1] == 'O':
                q.append([i, n-1])
                visited[i][n-1] = 1
        for i in range(n):
            if board[0][i] == 'O':
                q.append([0, i])
                visited[0][i] = 1
            if board[m-1][i] == 'O':
                q.append([m - 1, i])
                visited[m - 1][i] = 1
        while q:
            x, y = q.popleft()
            board[x][y] = 'F'
            for a, b in self.paths:
                nx, ny = x + a, y + b
                if 0 <= nx < m and 0 <= ny < n and board[nx][ny] == 'O' and not visited[nx][ny]:
                    q.append([nx, ny])
                    visited[nx][ny] = 1
        for i in range(m):
            for j in range(n):
                if board[i][j] == 'O':
                    board[i][j] = 'X'
                if board[i][j] == 'F':
                    board[i][j] = 'O'<jupyter_output><empty_output><jupyter_text>Solution 2. Using DFS (depth first search)<jupyter_code>from typing import List
class Solution:
    paths = [[-1, 0], [1, 0], [0, -1], [0, 1]]

    def dfs(self, board, i, j, m, n):
        if 0 <= i < m and 0 <= j < n and board[i][j] == 'O':
            board[i][j] = 'F'
            for x, y in self.paths:
                ni, nj = i + x, j + y
                board = self.dfs(board, ni, nj, m, n)
        return board
    
    def solve(self, board: List[List[str]]) -> None:
        """
        Do not return anything, modify board in-place instead.
        """
        if not board or not board[0]:
            return []
        m = len(board)
        n = len(board[0])
        # print(board)
        if m == 0 or n == 0:
            return []
        for i in range(m):
            if board[i][0] == 'O':
                board = self.dfs(board, i, 0, m, n)
            if board[i][n -1] == 'O':
                board = self.dfs(board, i, n - 1, m, n)
        for i in range(n):
            if board[0][i] == 'O':
                board = self.dfs(board, 0, i, m, n)
            if board[m-1][i] == 'O':
                board = self.dfs(board, m - 1, i, m, n)
        for i in range(m):
            for j in range(n):
                if board[i][j] == 'O':
                    board[i][j] = 'X'
                if board[i][j] == 'F':
                    board[i][j] = 'O'<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup