mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 15:45:40 +08:00
@ -561,6 +561,237 @@ function solve(board) {
|
||||
}
|
||||
```
|
||||
|
||||
### Go
|
||||
|
||||
dfs:
|
||||
|
||||
```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' {
|
||||
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'
|
||||
continue
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
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'
|
||||
continue
|
||||
}
|
||||
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})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Rust
|
||||
|
||||
bfs:
|
||||
|
||||
```rust
|
||||
impl Solution {
|
||||
const DIRECTIONS: [(isize, isize); 4] = [(0, 1), (0, -1), (1, 0), (-1, 0)];
|
||||
pub fn solve(board: &mut Vec<Vec<char>>) {
|
||||
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<char>], 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<Vec<char>>) {
|
||||
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<char>], 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<p align="center">
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
|
Reference in New Issue
Block a user