diff --git a/problems/0130.被围绕的区域.md b/problems/0130.被围绕的区域.md index e8a1f02f..8014c0c8 100644 --- a/problems/0130.被围绕的区域.md +++ b/problems/0130.被围绕的区域.md @@ -435,6 +435,132 @@ class Solution: ``` +### JavaScript +```JavaScript +/** + * @description 深度搜索优先 + * @param {character[][]} board + * @return {void} Do not return anything, modify board in-place instead. + */ +function solve(board) { + const dir = [[-1, 0], [1, 0], [0, -1], [0, 1]]; + const [rowSize, colSize] = [board.length, board[0].length]; + + function dfs(board, x, y) { + board[x][y] = 'A'; + for (let i = 0; i < 4; i++) { + const nextX = dir[i][0] + x; + const nextY = dir[i][1] + y; + if (nextX < 0 || nextX >= rowSize || nextY < 0 || nextY >= colSize) { + continue; + } + if (board[nextX][nextY] === 'O') { + dfs(board, nextX, nextY); + } + } + } + + for (let i = 0; i < rowSize; i++) { + if (board[i][0] === 'O') { + dfs(board, i, 0); + } + if (board[i][colSize - 1] === 'O') { + dfs(board, i, colSize - 1); + } + } + + for (let i = 1; i < colSize - 1; i++) { + if (board[0][i] === 'O') { + dfs(board, 0, i); + } + if (board[rowSize - 1][i] === 'O') { + dfs(board, rowSize - 1, i); + } + } + + for (let i = 0; i < rowSize; i++) { + for (let k = 0; k < colSize; k++) { + if (board[i][k] === 'A') { + board[i][k] = 'O'; + } else if (board[i][k] === 'O') { + board[i][k] = 'X'; + } + } + } +} + +/** + * @description 广度搜索优先 + * @param {character[][]} board + * @return {void} Do not return anything, modify board in-place instead. + */ +function solve(board) { + const dir = [[-1, 0], [1, 0], [0, -1], [0, 1]]; + const [rowSize, colSize] = [board.length, board[0].length]; + + function bfs(board, x, y) { + board[x][y] = 'A'; + const stack = [y, x]; + + while (stack.length !== 0) { + const top = [stack.pop(), stack.pop()]; + for (let i = 0; i < 4; i++) { + const nextX = dir[i][0] + top[0]; + const nextY = dir[i][1] + top[1]; + + if (nextX < 0 || nextX >= rowSize || nextY < 0 || nextY >= colSize) { + continue; + } + + if (board[nextX][nextY] === 'O') { + board[nextX][nextY] = 'A'; + stack.push(nextY, nextX); + } + } + } + + for (let i = 0; i < 4; i++) { + const nextX = dir[i][0] + x; + const nextY = dir[i][1] + y; + if (nextX < 0 || nextX >= rowSize || nextY < 0 || nextY >= colSize) { + continue; + } + if (board[nextX][nextY] === 'O') { + dfs(board, nextX, nextY); + } + } + } + + for (let i = 0; i < rowSize; i++) { + if (board[i][0] === 'O') { + bfs(board, i, 0); + } + if (board[i][colSize - 1] === 'O') { + bfs(board, i, colSize - 1); + } + } + + for (let i = 1; i < colSize - 1; i++) { + if (board[0][i] === 'O') { + bfs(board, 0, i); + } + if (board[rowSize - 1][i] === 'O') { + bfs(board, rowSize - 1, i); + } + } + + for (let i = 0; i < rowSize; i++) { + for (let k = 0; k < colSize; k++) { + if (board[i][k] === 'A') { + board[i][k] = 'O'; + } else if (board[i][k] === 'O') { + board[i][k] = 'X'; + } + } + } +} +``` +