Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5ad279f

Browse files
authored
Update 0130.被围绕的区域.md for rust
1 parent 52496f8 commit 5ad279f

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed

‎problems/0130.被围绕的区域.md‎

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,7 @@ func solve(board [][]byte) {
593593
for j, c := range r {
594594
if c == 'A' {
595595
r[j] = 'O'
596+
continue
596597
}
597598
if c == 'O' {
598599
r[j] = 'X'
@@ -645,6 +646,7 @@ func solve(board [][]byte) {
645646
for j, c := range r {
646647
if c == 'A' {
647648
r[j] = 'O'
649+
continue
648650
}
649651
if c == 'O' {
650652
r[j] = 'X'
@@ -673,6 +675,123 @@ func bfs(board [][]byte, i, j int) {
673675
}
674676
```
675677

678+
### Rust
679+
680+
bfs:
681+
682+
```rust
683+
impl Solution {
684+
const DIRECTIONS: [(isize, isize); 4] = [(0, 1), (0, -1), (1, 0), (-1, 0)];
685+
pub fn solve(board: &mut Vec<Vec<char>>) {
686+
let (rows, cols) = (board.len(), board[0].len());
687+
//
688+
for i in 0..rows {
689+
if board[i][0] == 'O' {
690+
Self::dfs(board, i, 0);
691+
}
692+
if board[i][cols - 1] == 'O' {
693+
Self::dfs(board, i, cols - 1);
694+
}
695+
}
696+
//
697+
for j in 0..cols {
698+
if board[0][j] == 'O' {
699+
Self::dfs(board, 0, j);
700+
}
701+
if board[rows - 1][j] == 'O' {
702+
Self::dfs(board, rows - 1, j);
703+
}
704+
}
705+
706+
for v in board.iter_mut() {
707+
for c in v.iter_mut() {
708+
if *c == 'A' {
709+
*c = 'O';
710+
continue;
711+
}
712+
if *c == 'O' {
713+
*c = 'X';
714+
}
715+
}
716+
}
717+
}
718+
719+
pub fn dfs(board: &mut [Vec<char>], i: usize, j: usize) {
720+
board[i][j] = 'A';
721+
for (d1, d2) in Self::DIRECTIONS {
722+
let (x, y) = (i as isize + d1, j as isize + d2);
723+
if x < 0 || x >= board.len() as isize || y < 0 || y >= board[0].len() as isize {
724+
continue;
725+
}
726+
let (x, y) = (x as usize, y as usize);
727+
if board[x][y] == 'O' {
728+
Self::dfs(board, x, y);
729+
}
730+
}
731+
}
732+
}
733+
```
734+
735+
bfs:
736+
737+
```rust
738+
use std::collections::VecDeque;
739+
impl Solution {
740+
const DIRECTIONS: [(isize, isize); 4] = [(0, 1), (0, -1), (1, 0), (-1, 0)];
741+
pub fn solve(board: &mut Vec<Vec<char>>) {
742+
let (rows, cols) = (board.len(), board[0].len());
743+
//
744+
for i in 0..rows {
745+
if board[i][0] == 'O' {
746+
Self::bfs(board, i, 0);
747+
}
748+
if board[i][cols - 1] == 'O' {
749+
Self::bfs(board, i, cols - 1);
750+
}
751+
}
752+
//
753+
for j in 0..cols {
754+
if board[0][j] == 'O' {
755+
Self::bfs(board, 0, j);
756+
}
757+
if board[rows - 1][j] == 'O' {
758+
Self::bfs(board, rows - 1, j);
759+
}
760+
}
761+
762+
for v in board.iter_mut() {
763+
for c in v.iter_mut() {
764+
if *c == 'A' {
765+
*c = 'O';
766+
continue;
767+
}
768+
if *c == 'O' {
769+
*c = 'X';
770+
}
771+
}
772+
}
773+
}
774+
775+
pub fn bfs(board: &mut [Vec<char>], i: usize, j: usize) {
776+
let mut queue = VecDeque::from([(i, j)]);
777+
board[i][j] = 'A';
778+
while let Some((i, j)) = queue.pop_front() {
779+
for (d1, d2) in Self::DIRECTIONS {
780+
let (x, y) = (i as isize + d1, j as isize + d2);
781+
if x < 0 || x >= board.len() as isize || y < 0 || y >= board[0].len() as isize {
782+
continue;
783+
}
784+
let (x, y) = (x as usize, y as usize);
785+
if board[x][y] == 'O' {
786+
board[x][y] = 'A';
787+
queue.push_back((x, y));
788+
}
789+
}
790+
}
791+
}
792+
}
793+
```
794+
676795
<p align="center">
677796
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
678797
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /