题目描述:
方法一:dfs
class Solution: def solve(self, board: List[List[str]]) -> None: """ Do not return anything, modify board in-place instead. """ if not board or len(board)<=2 or len(board[0])<=2: return row = len(board) col = len(board[0]) def dfs(i,j): if i < 0 or j < 0 or i >= row or j >= col or board[i][j] != ‘O‘: return board[i][j] = ‘#‘ dfs(i-1,j) dfs(i+1,j) dfs(i,j+1) dfs(i,j-1) for i in range(row): dfs(i,0) dfs(i,col-1) for j in range(col): dfs(0,j) dfs(row-1,j) for i in range(0, row): for j in range(0, col): if board[i][j] == ‘O‘: board[i][j] = ‘X‘ if board[i][j] == ‘#‘: board[i][j] = ‘O‘
方法二:并查集
略
原文地址:https://www.cnblogs.com/oldby/p/11194038.html
时间: 2024-10-12 23:20:30