Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 70a73d2

Browse files
committed
solve #130
1 parent 323fa69 commit 70a73d2

File tree

2 files changed

+171
-0
lines changed

2 files changed

+171
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,4 @@ mod n0126_word_ladder_ii;
128128
mod n0127_word_ladder;
129129
mod n0128_longest_consecutive_sequence;
130130
mod n0129_sum_root_to_leaf_numbers;
131+
mod n0130_surrounded_regions;

src/n0130_surrounded_regions.rs

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
/**
2+
* [130] Surrounded Regions
3+
*
4+
* Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'.
5+
*
6+
* A region is captured by flipping all 'O's into 'X's in that surrounded region.
7+
*
8+
* Example:
9+
*
10+
*
11+
* X X X X
12+
* X O O X
13+
* X X O X
14+
* X O X X
15+
*
16+
*
17+
* After running your function, the board should be:
18+
*
19+
*
20+
* X X X X
21+
* X X X X
22+
* X X X X
23+
* X O X X
24+
*
25+
*
26+
* Explanation:
27+
*
28+
* Surrounded regions shouldn’t be on the border, which means that any 'O' on the border of the board are not flipped to 'X'. Any 'O' that is not on the border and it is not connected to an 'O' on the border will be flipped to 'X'. Two cells are connected if they are adjacent cells connected horizontally or vertically.
29+
*
30+
*/
31+
pub struct Solution {}
32+
33+
// submission codes start here
34+
35+
/*
36+
从最外层开始, 基于为 'O' 的格子做 DFS, 将与边界连接的所有 'O' 标记为一个特殊 char, 最后将没有标记到的 'O' 全部标记为 'X'
37+
*/
38+
impl Solution {
39+
pub fn solve(board: &mut Vec<Vec<char>>) {
40+
if board.is_empty() || board[0].is_empty() { return }
41+
let (height, width) = (board.len(), board[0].len());
42+
// 遍历最外层的 4 条边
43+
for j in 0..width {
44+
Solution::dfs(0, j, height, width, board);
45+
Solution::dfs(height-1, j, height, width, board);
46+
}
47+
for i in 1..height-1 {
48+
Solution::dfs(i, 0, height, width, board);
49+
Solution::dfs(i, width-1, height, width, board);
50+
}
51+
for k in 0..height*width {
52+
board[k/width][k%width] = if board[k/width][k%width] == '_' {
53+
'O'
54+
} else {
55+
'X'
56+
}
57+
}
58+
}
59+
60+
fn dfs(i: usize, j: usize, height: usize, width: usize, board: &mut Vec<Vec<char>>) {
61+
if board[i][j] == 'O' {
62+
board[i][j] = '_';
63+
if i > 1 { Solution::dfs(i-1, j, height, width, board) }
64+
if j > 1 { Solution::dfs(i, j-1, height, width, board) }
65+
if i + 1 < height { Solution::dfs(i+1, j, height, width, board) }
66+
if j + 1 < width { Solution::dfs(i, j+1, height, width, board) }
67+
}
68+
}
69+
}
70+
71+
// submission codes end
72+
73+
#[cfg(test)]
74+
mod tests {
75+
use super::*;
76+
77+
#[test]
78+
fn test_130() {
79+
let mut matrix = vec![
80+
vec!['X','X','X','X'],
81+
vec!['X','O','O','X'],
82+
vec!['X','X','O','X'],
83+
vec!['X','O','X','X'],
84+
];
85+
Solution::solve(&mut matrix);
86+
assert_eq!(
87+
matrix,
88+
vec![
89+
vec!['X','X','X','X'],
90+
vec!['X','X','X','X'],
91+
vec!['X','X','X','X'],
92+
vec!['X','O','X','X'],
93+
]
94+
);
95+
96+
let mut matrix = vec![
97+
vec!['X','X','X','X'],
98+
vec!['X','O','O','X'],
99+
vec!['X','O','O','X'],
100+
vec!['X','X','X','X'],
101+
];
102+
Solution::solve(&mut matrix);
103+
assert_eq!(
104+
matrix,
105+
vec![
106+
vec!['X','X','X','X'],
107+
vec!['X','X','X','X'],
108+
vec!['X','X','X','X'],
109+
vec!['X','X','X','X'],
110+
]
111+
);
112+
113+
let mut matrix = vec![
114+
vec!['X','X','X','X'],
115+
vec!['O','X','O','X'],
116+
vec!['O','X','O','X'],
117+
vec!['X','O','X','X'],
118+
];
119+
Solution::solve(&mut matrix);
120+
assert_eq!(
121+
matrix,
122+
vec![
123+
vec!['X','X','X','X'],
124+
vec!['O','X','X','X'],
125+
vec!['O','X','X','X'],
126+
vec!['X','O','X','X'],
127+
]
128+
);
129+
130+
let mut matrix = vec![
131+
vec!['X','X','X','X','O','X'],
132+
vec!['O','X','X','O','O','X'],
133+
vec!['X','O','X','O','O','O'],
134+
vec!['X','O','O','O','X','O'],
135+
vec!['O','O','X','X','O','X'],
136+
vec!['X','O','X','O','X','X'],
137+
];
138+
Solution::solve(&mut matrix);
139+
assert_eq!(
140+
matrix,
141+
vec![
142+
vec!['X','X','X','X','O','X'],
143+
vec!['O','X','X','O','O','X'],
144+
vec!['X','O','X','O','O','O'],
145+
vec!['X','O','O','O','X','O'],
146+
vec!['O','O','X','X','X','X'],
147+
vec!['X','O','X','O','X','X'],
148+
]
149+
);
150+
151+
let mut matrix = vec![
152+
vec!['X','X','X','X','X','X','X','X','X','X','X','X','X','X','X','X','X','X','X','X'],
153+
vec!['X','X','X','X','X','X','X','X','X','O','O','O','X','X','X','X','X','X','X','X'],
154+
vec!['X','X','X','X','X','O','O','O','X','O','X','O','X','X','X','X','X','X','X','X'],
155+
vec!['X','X','X','X','X','O','X','O','X','O','X','O','O','O','X','X','X','X','X','X'],
156+
vec!['X','X','X','X','X','O','X','O','O','O','X','X','X','X','X','X','X','X','X','X'],
157+
vec!['X','X','X','X','X','O','X','X','X','X','X','X','X','X','X','X','X','X','X','X']];
158+
Solution::solve(&mut matrix);
159+
assert_eq!(
160+
matrix,
161+
vec![
162+
vec!['X','X','X','X','X','X','X','X','X','X','X','X','X','X','X','X','X','X','X','X'],
163+
vec!['X','X','X','X','X','X','X','X','X','O','O','O','X','X','X','X','X','X','X','X'],
164+
vec!['X','X','X','X','X','O','O','O','X','O','X','O','X','X','X','X','X','X','X','X'],
165+
vec!['X','X','X','X','X','O','X','O','X','O','X','O','O','O','X','X','X','X','X','X'],
166+
vec!['X','X','X','X','X','O','X','O','O','O','X','X','X','X','X','X','X','X','X','X'],
167+
vec!['X','X','X','X','X','O','X','X','X','X','X','X','X','X','X','X','X','X','X','X']]
168+
);
169+
}
170+
}

0 commit comments

Comments
 (0)