voiddfs(vector<vector<char>>& board, int x, int y){ if (x < 0 || x >= n || y < 0 || y >= m || board[x][y] != 'O') { return; } board[x][y] = 'A'; dfs(board, x + 1, y); dfs(board, x - 1, y); dfs(board, x, y + 1); dfs(board, x, y - 1); }
voidsolve(vector<vector<char>>& board){ n = board.size(); if (n == 0) { return; } m = board[0].size(); for (int i = 0; i < n; i++) { dfs(board, i, 0); dfs(board, i, m - 1); } for (int i = 1; i < m - 1; i++) { dfs(board, 0, i); dfs(board, n - 1, i); } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (board[i][j] == 'A') { board[i][j] = 'O'; } elseif (board[i][j] == 'O') { board[i][j] = 'X'; } } } } };
publicvoidsolve(char[][] board) { n = board.length; if (n == 0) { return; } m = board[0].length; for (inti=0; i < n; i++) { dfs(board, i, 0); dfs(board, i, m - 1); } for (inti=1; i < m - 1; i++) { dfs(board, 0, i); dfs(board, n - 1, i); } for (inti=0; i < n; i++) { for (intj=0; j < m; j++) { if (board[i][j] == 'A') { board[i][j] = 'O'; } elseif (board[i][j] == 'O') { board[i][j] = 'X'; } } } }
publicvoiddfs(char[][] board, int x, int y) { if (x < 0 || x >= n || y < 0 || y >= m || board[x][y] != 'O') { return; } board[x][y] = 'A'; dfs(board, x + 1, y); dfs(board, x - 1, y); dfs(board, x, y + 1); dfs(board, x, y - 1); } }
classSolution: defsolve(self, board: List[List[str]]) -> None: ifnot board: return n, m = len(board), len(board[0])
defdfs(x, y): ifnot0 <= x < n ornot0 <= y < m or board[x][y] != 'O': return board[x][y] = "A" dfs(x + 1, y) dfs(x - 1, y) dfs(x, y + 1) dfs(x, y - 1) for i inrange(n): dfs(i, 0) dfs(i, m - 1) for i inrange(m - 1): dfs(0, i) dfs(n - 1, i) for i inrange(n): for j inrange(m): if board[i][j] == "A": board[i][j] = "O" elif board[i][j] == "O": board[i][j] = "X"
funcsolve(board [][]byte) { iflen(board) == 0 || len(board[0]) == 0 { return } n, m = len(board), len(board[0]) for i := 0; i < n; i++ { dfs(board, i, 0) dfs(board, i, m - 1) } for i := 1; i < m - 1; i++ { dfs(board, 0, i) dfs(board, n - 1, i) } for i := 0; i < n; i++ { for j := 0; j < m; j++ { if board[i][j] == 'A' { board[i][j] = 'O' } elseif board[i][j] == 'O' { board[i][j] = 'X' } } } }
funcdfs(board [][]byte, x, y int) { if x < 0 || x >= n || y < 0 || y >= m || board[x][y] != 'O' { return } board[x][y] = 'A' dfs(board, x + 1, y) dfs(board, x - 1, y) dfs(board, x, y + 1) dfs(board, x, y - 1) }
classSolution: defsolve(self, board: List[List[str]]) -> None: ifnot board: return n, m = len(board), len(board[0]) que = collections.deque() for i inrange(n): if board[i][0] == "O": que.append((i, 0)) board[i][0] = "A" if board[i][m - 1] == "O": que.append((i, m - 1)) board[i][m - 1] = "A" for i inrange(m - 1): if board[0][i] == "O": que.append((0, i)) board[0][i] = "A" if board[n - 1][i] == "O": que.append((n - 1, i)) board[n - 1][i] = "A" while que: x, y = que.popleft() for mx, my in [(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)]: if0 <= mx < n and0 <= my < m and board[mx][my] == "O": que.append((mx, my)) board[mx][my] = "A" for i inrange(n): for j inrange(m): if board[i][j] == "A": board[i][j] = "O" elif board[i][j] == "O": board[i][j] = "X"
voidsolve(char** board, int boardSize, int* boardColSize) { int n = boardSize; if (n == 0) { return; } int m = boardColSize[0];
int** que = (int**)malloc(sizeof(int*) * n * m); for (int i = 0; i < n * m; i++) { que[i] = (int*)malloc(sizeof(int) * 2); } int l = 0, r = 0; for (int i = 0; i < n; i++) { if (board[i][0] == 'O') { board[i][0] = 'A'; que[r][0] = i, que[r++][1] = 0; } if (board[i][m - 1] == 'O') { board[i][m - 1] = 'A'; que[r][0] = i, que[r++][1] = m - 1; } } for (int i = 1; i < m - 1; i++) { if (board[0][i] == 'O') { board[0][i] = 'A'; que[r][0] = 0, que[r++][1] = i; } if (board[n - 1][i] == 'O') { board[n - 1][i] = 'A'; que[r][0] = n - 1, que[r++][1] = i; } } while (l < r) { int x = que[l][0], y = que[l][1]; l++; for (int i = 0; i < 4; i++) { int mx = x + dx[i], my = y + dy[i]; if (mx < 0 || my < 0 || mx >= n || my >= m || board[mx][my] != 'O') { continue; } board[mx][my] = 'A'; que[r][0] = mx, que[r++][1] = my; } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (board[i][j] == 'A') { board[i][j] = 'O'; } elseif (board[i][j] == 'O') { board[i][j] = 'X'; } } } for (int i = 0; i < n * m; i++) { free(que[i]); } free(que); }