-
Notifications
You must be signed in to change notification settings - Fork 0
/
surrounded_regions.rs
45 lines (38 loc) · 972 Bytes
/
surrounded_regions.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#![allow(dead_code)]
pub fn solve(board: &mut Vec<Vec<char>>) {
let rows = board.len();
let cols = board[0].len();
fn dfs(board: &mut Vec<Vec<char>>, r: usize, c: usize) {
let rows = board.len();
let cols = board[0].len();
if r >= rows || c >= cols || board[r][c] != 'O' {
return;
}
board[r][c] = '#';
if r > 0 {
dfs(board, r - 1, c);
}
if c > 0 {
dfs(board, r, c - 1);
}
dfs(board, r + 1, c);
dfs(board, r, c + 1);
}
for r in 0..rows {
dfs(board, r, 0);
dfs(board, r, cols - 1);
}
for c in 0..cols {
dfs(board, 0, c);
dfs(board, rows - 1, c);
}
for r in 0..rows {
for c in 0..cols {
if board[r][c] == '#' {
board[r][c] = 'O';
} else if board[r][c] == 'O' {
board[r][c] = 'X';
}
}
}
}