From 73c06613f7f9614df170667aa6bef87f0dcca3d4 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Mon, 15 Jan 2024 17:24:43 +0800 Subject: [PATCH 1/3] =?UTF-8?q?Update=200130.=E8=A2=AB=E5=9B=B4=E7=BB=95?= =?UTF-8?q?=E7=9A=84=E5=8C=BA=E5=9F=9F.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0130.被围绕的区域.md | 52 +++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/problems/0130.被围绕的区域.md b/problems/0130.被围绕的区域.md index 8014c0c8..e2185a17 100644 --- a/problems/0130.被围绕的区域.md +++ b/problems/0130.被围绕的区域.md @@ -561,6 +561,58 @@ function solve(board) { } ``` +### Go + +```dfs +var DIRECTIONS = [4][2]int{{-1, 0}, {0, -1}, {1, 0}, {0, 1}} + +func solve(board [][]byte) { + rows, cols := len(board), len(board[0]) + // 列 + for i := 0; i < rows; i++ { + if board[i][0] == 'O' { + dfs(board, i, 0) + } + if board[i][cols-1] == 'O' { + dfs(board, i, cols-1) + } + } + // 行 + for j := 0; j < cols; j++ { + if board[0][j] == 'O' { + dfs(board, 0, j) + } + if board[rows-1][j] == 'O' { + dfs(board, rows-1, j) + } + } + + for _, r := range board { + for j, c := range r { + if c == 'A' { + r[j] = 'O' + } + if c == 'O' { + r[j] = 'X' + } + } + } +} + +func dfs(board [][]byte, i, j int) { + board[i][j] = 'A' + for _, d := range DIRECTIONS { + x, y := i+d[0], j+d[1] + if x < 0 || x >= len(board) || y < 0 || y >= len(board[0]) { + continue + } + if board[x][y] == 'O' { + dfs(board, x, y) + } + } +} +``` +

From 52496f8fd6e65aa5073f48fcc6a545f18dffd218 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Mon, 15 Jan 2024 17:29:34 +0800 Subject: [PATCH 2/3] =?UTF-8?q?Update=200130.=E8=A2=AB=E5=9B=B4=E7=BB=95?= =?UTF-8?q?=E7=9A=84=E5=8C=BA=E5=9F=9F.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0130.被围绕的区域.md | 62 ++++++++++++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) diff --git a/problems/0130.被围绕的区域.md b/problems/0130.被围绕的区域.md index e2185a17..352face8 100644 --- a/problems/0130.被围绕的区域.md +++ b/problems/0130.被围绕的区域.md @@ -563,7 +563,9 @@ function solve(board) { ### Go -```dfs +dfs: + +```go var DIRECTIONS = [4][2]int{{-1, 0}, {0, -1}, {1, 0}, {0, 1}} func solve(board [][]byte) { @@ -613,6 +615,64 @@ func dfs(board [][]byte, i, j int) { } ``` +bfs: + +```go +var DIRECTIONS = [4][2]int{{-1, 0}, {0, -1}, {1, 0}, {0, 1}} + +func solve(board [][]byte) { + rows, cols := len(board), len(board[0]) + // 列 + for i := 0; i < rows; i++ { + if board[i][0] == 'O' { + bfs(board, i, 0) + } + if board[i][cols-1] == 'O' { + bfs(board, i, cols-1) + } + } + // 行 + for j := 0; j < cols; j++ { + if board[0][j] == 'O' { + bfs(board, 0, j) + } + if board[rows-1][j] == 'O' { + bfs(board, rows-1, j) + } + } + + for _, r := range board { + for j, c := range r { + if c == 'A' { + r[j] = 'O' + } + if c == 'O' { + r[j] = 'X' + } + } + } +} + +func bfs(board [][]byte, i, j int) { + queue := [][]int{{i, j}} + board[i][j] = 'A' + for len(queue) > 0 { + cur := queue[0] + queue = queue[1:] + for _, d := range DIRECTIONS { + x, y := cur[0]+d[0], cur[1]+d[1] + if x < 0 || x >= len(board) || y < 0 || y >= len(board[0]) { + continue + } + if board[x][y] == 'O' { + board[x][y] = 'A' + queue = append(queue, []int{x, y}) + } + } + } +} +``` +

From 5ad279f73717ba8c7194a0b238a8bb5a0d97b2e7 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Mon, 15 Jan 2024 18:11:30 +0800 Subject: [PATCH 3/3] =?UTF-8?q?Update=200130.=E8=A2=AB=E5=9B=B4=E7=BB=95?= =?UTF-8?q?=E7=9A=84=E5=8C=BA=E5=9F=9F.md=20for=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0130.被围绕的区域.md | 119 ++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) diff --git a/problems/0130.被围绕的区域.md b/problems/0130.被围绕的区域.md index 352face8..1ddaaa7f 100644 --- a/problems/0130.被围绕的区域.md +++ b/problems/0130.被围绕的区域.md @@ -593,6 +593,7 @@ func solve(board [][]byte) { for j, c := range r { if c == 'A' { r[j] = 'O' + continue } if c == 'O' { r[j] = 'X' @@ -645,6 +646,7 @@ func solve(board [][]byte) { for j, c := range r { if c == 'A' { r[j] = 'O' + continue } if c == 'O' { r[j] = 'X' @@ -673,6 +675,123 @@ func bfs(board [][]byte, i, j int) { } ``` +### Rust + +bfs: + +```rust +impl Solution { + const DIRECTIONS: [(isize, isize); 4] = [(0, 1), (0, -1), (1, 0), (-1, 0)]; + pub fn solve(board: &mut Vec>) { + let (rows, cols) = (board.len(), board[0].len()); + // 列 + for i in 0..rows { + if board[i][0] == 'O' { + Self::dfs(board, i, 0); + } + if board[i][cols - 1] == 'O' { + Self::dfs(board, i, cols - 1); + } + } + //行 + for j in 0..cols { + if board[0][j] == 'O' { + Self::dfs(board, 0, j); + } + if board[rows - 1][j] == 'O' { + Self::dfs(board, rows - 1, j); + } + } + + for v in board.iter_mut() { + for c in v.iter_mut() { + if *c == 'A' { + *c = 'O'; + continue; + } + if *c == 'O' { + *c = 'X'; + } + } + } + } + + pub fn dfs(board: &mut [Vec], i: usize, j: usize) { + board[i][j] = 'A'; + for (d1, d2) in Self::DIRECTIONS { + let (x, y) = (i as isize + d1, j as isize + d2); + if x < 0 || x >= board.len() as isize || y < 0 || y >= board[0].len() as isize { + continue; + } + let (x, y) = (x as usize, y as usize); + if board[x][y] == 'O' { + Self::dfs(board, x, y); + } + } + } +} +``` + +bfs: + +```rust +use std::collections::VecDeque; +impl Solution { + const DIRECTIONS: [(isize, isize); 4] = [(0, 1), (0, -1), (1, 0), (-1, 0)]; + pub fn solve(board: &mut Vec>) { + let (rows, cols) = (board.len(), board[0].len()); + // 列 + for i in 0..rows { + if board[i][0] == 'O' { + Self::bfs(board, i, 0); + } + if board[i][cols - 1] == 'O' { + Self::bfs(board, i, cols - 1); + } + } + //行 + for j in 0..cols { + if board[0][j] == 'O' { + Self::bfs(board, 0, j); + } + if board[rows - 1][j] == 'O' { + Self::bfs(board, rows - 1, j); + } + } + + for v in board.iter_mut() { + for c in v.iter_mut() { + if *c == 'A' { + *c = 'O'; + continue; + } + if *c == 'O' { + *c = 'X'; + } + } + } + } + + pub fn bfs(board: &mut [Vec], i: usize, j: usize) { + let mut queue = VecDeque::from([(i, j)]); + board[i][j] = 'A'; + while let Some((i, j)) = queue.pop_front() { + for (d1, d2) in Self::DIRECTIONS { + let (x, y) = (i as isize + d1, j as isize + d2); + if x < 0 || x >= board.len() as isize || y < 0 || y >= board[0].len() as isize { + continue; + } + let (x, y) = (x as usize, y as usize); + if board[x][y] == 'O' { + board[x][y] = 'A'; + queue.push_back((x, y)); + } + } + } + } +} +``` +