MediumRating 1706
130. Surrounded Regions
arraydepth-first-searchbreadth-first-searchunion-findmatrix
解題說明
C++ 解法
複雜度分析
虛擬碼
1. For each cell on the 4 borders: a. If cell == 'O': run DFS to mark all connected 'O's as 'S' 2. DFS(r, c): a. If out of bounds or board[r][c] != 'O': return b. Set board[r][c] = 'S' c. Recurse in 4 directions 3. Scan entire board: a. 'O' -> 'X' (surrounded, flip) b. 'S' -> 'O' (safe, restore) c. 'X' -> 'X' (unchanged)