mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 20:40:39 +08:00
Update 0130.被围绕的区域.md for rust
This commit is contained in:
@ -593,6 +593,7 @@ func solve(board [][]byte) {
|
|||||||
for j, c := range r {
|
for j, c := range r {
|
||||||
if c == 'A' {
|
if c == 'A' {
|
||||||
r[j] = 'O'
|
r[j] = 'O'
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
if c == 'O' {
|
if c == 'O' {
|
||||||
r[j] = 'X'
|
r[j] = 'X'
|
||||||
@ -645,6 +646,7 @@ func solve(board [][]byte) {
|
|||||||
for j, c := range r {
|
for j, c := range r {
|
||||||
if c == 'A' {
|
if c == 'A' {
|
||||||
r[j] = 'O'
|
r[j] = 'O'
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
if c == 'O' {
|
if c == 'O' {
|
||||||
r[j] = 'X'
|
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<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">
|
<p align="center">
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||||
|
Reference in New Issue
Block a user