-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths0130_surrounded_regions.rs
More file actions
121 lines (101 loc) · 3.13 KB
/
s0130_surrounded_regions.rs
File metadata and controls
121 lines (101 loc) · 3.13 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#![allow(unused)]
pub struct Solution {}
use std::collections::VecDeque;
const DIRECTIONS: [(i32, i32); 4] = [(1, 0), (-1, 0), (0, 1), (0, -1)];
const BFS: bool = true;
impl Solution {
//Time O(N) Space O(N) N is the number of cells in the board
pub fn solve(board: &mut Vec<Vec<char>>) {
if board.len() < 1 {
return;
}
let rows = board.len();
let cols = board[0].len();
let mut borders = vec![];
// step1 construct the list of border cells
for r in 0..rows {
borders.push((r, 0));
borders.push((r, cols - 1));
}
for c in 0..cols {
borders.push((0, c));
borders.push((rows - 1, c));
}
// step2 mark the escaped cells
for (x, y) in borders.iter() {
if board[*x][*y] != 'O' {
continue;
}
if BFS {
Self::bfs(board, *x, *y, &rows, &cols);
} else {
Self::dfs(board, *x, *y, &rows, &cols);
}
}
// step3 flip the cells to their correct final states
for r in 0..rows {
for c in 0..cols {
if board[r][c] == 'O' {
board[r][c] = 'X';
}
if board[r][c] == 'E' {
board[r][c] = 'O';
}
}
}
}
fn dfs(board: &mut Vec<Vec<char>>, row: usize, col: usize, rows: &usize, cols: &usize) {
if board[row][col] != 'O' {
return;
}
// mark Visited in place
board[row][col] = 'E';
for direction in DIRECTIONS.iter() {
let r = (row as i32 + direction.0) as usize;
let c = (col as i32 + direction.1) as usize;
if c < *cols - 1 && r < *rows - 1 && c > 0 && r > 0 {
Self::dfs(board, r, c, rows, cols);
}
}
}
fn bfs(board: &mut Vec<Vec<char>>, row: usize, col: usize, rows: &usize, cols: &usize) {
let mut queue = VecDeque::new();
queue.push_back((row, col));
while let Some((row, col)) = queue.pop_front() {
if board[row][col] != 'O' {
continue;
}
board[row][col] = 'E';
for direction in DIRECTIONS.iter() {
let r = (row as i32 + direction.0) as usize;
let c = (col as i32 + direction.1) as usize;
if c < *cols - 1 && r < *rows - 1 && c > 0 && r > 0 {
queue.push_back((r, c));
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_130() {
let mut board = vec![
vec!['X', 'X', 'X', 'X'],
vec!['X', 'O', 'O', 'X'],
vec!['X', 'X', 'O', 'X'],
vec!['X', 'O', 'X', 'X'],
];
Solution::solve(&mut board);
assert_eq!(
board,
vec![
vec!['X', 'X', 'X', 'X'],
vec!['X', 'X', 'X', 'X'],
vec!['X', 'X', 'X', 'X'],
vec!['X', 'O', 'X', 'X'],
]
);
}
}