/
resolve.rs
56 lines (56 loc) · 1.88 KB
/
resolve.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
impl Solution {
pub fn solve(board: &mut Vec<Vec<char>>) {
if board.len() != 0 {
let (m, n) = (board.len(), board[0].len());
let list = board
.iter()
.enumerate()
.map(|(i, row)| {
row.iter()
.enumerate()
.filter(|(j, x)| {
(i == 0 || j == &0 || i == m - 1 || *j == n - 1) && **x == 'O'
})
.map(|(j, _x)| (i, j))
.collect::<Vec<(usize, usize)>>()
})
.flatten()
.collect::<Vec<(usize, usize)>>();
list.iter().for_each(|(x, y)| {
find_list(board, *x, *y, m, n);
});
fn find_list(board: &mut Vec<Vec<char>>, x: usize, y: usize, m: usize, n: usize) {
board[x][y] = 'D';
if x < m - 1 {
if board[x + 1][y] == 'O' {
find_list(board, x + 1, y, m, n);
}
}
if x>0{
if board[x - 1][y] == 'O' {
find_list(board, x - 1, y, m, n);
}
}
if y<n-1{
if board[x][y + 1] == 'O' {
find_list(board, x, y + 1, m, n);
}
}
if y>0{
if board[x][y - 1] == 'O' {
find_list(board, x, y - 1, m, n);
}
}
}
for i in 0..m {
for j in 0..n {
if board[i][j] != 'D' {
board[i][j] = 'X'
} else {
board[i][j] = 'O'
}
}
}
}
}
}