-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsurrounded_regions.rs
120 lines (107 loc) · 3.14 KB
/
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
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
/// You are given an `m x n` matrix `board` containing letters `X` and `O`, capture regions that
/// are surrounded:
///
/// * Connect: A cell is connected to adjacent cells horizontally or vertically.
///
/// * Region: To form a region connect every `'O'` cell.
///
/// * Surround: The region is surrounded with `'X'` cells if you can connect the region with `'X'`
/// cells and none of the region cells are on the edge of the board.
///
/// A surrounded region is captured by replacing all `'O'`s with `'X'`s in the input matrix
/// `board`.
struct Solution;
impl Solution {
fn capture(board: &mut Vec<Vec<char>>, i: usize, j: usize) {
let m = board.len();
let n = board[0].len();
if board[i][j] == 'O' {
board[i][j] = 'C';
if i > 0 {
Self::capture(board, i - 1, j);
}
if j > 0 {
Self::capture(board, i, j - 1);
}
if i < m-1 {
Self::capture(board, i + 1, j);
}
if j < n-1 {
Self::capture(board, i, j + 1);
}
}
}
pub fn solve(board: &mut Vec<Vec<char>>) {
let m = board.len();
let n = board[0].len();
for i in 0..m {
for j in 0..n {
if board[i][j] == 'O' {
if i == 0 || i == m-1 {
Self::capture(board, i, j);
} else if j == 0 || j == n-1 {
Self::capture(board, i, j);
}
}
}
}
for i in 0..m {
for j in 0..n {
let value = board[i][j];
if value == 'O' {
board[i][j] = 'X';
} else if value == 'C' {
board[i][j] = 'O';
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::Solution;
#[test]
fn example_1() {
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'],
]);
}
#[test]
fn example_2() {
let mut board = vec![
vec!['X'],
];
Solution::solve(&mut board);
assert_eq!(board, vec![
vec!['X'],
])
}
#[test]
fn example_3() {
let mut board = vec![
vec!['O', 'X', 'X', 'O', 'X'],
vec!['X', 'O', 'O', 'X', 'O'],
vec!['X', 'O', 'X', 'O', 'X'],
vec!['O', 'X', 'O', 'O', 'O'],
vec!['X', 'X', 'O', 'X', 'O'],
];
Solution::solve(&mut board);
assert_eq!(board, vec![
vec!['O', 'X', 'X', 'O', 'X'],
vec!['X', 'X', 'X', 'X', 'O'],
vec!['X', 'X', 'X', 'O', 'X'],
vec!['O', 'X', 'O', 'O', 'O'],
vec!['X', 'X', 'O', 'X', 'O'],
]);
}
}