forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.rs
24 lines (24 loc) · 949 Bytes
/
Solution.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
impl Solution {
fn dfs(image: &mut Vec<Vec<i32>>, sr: i32, sc: i32, new_color: i32, target: i32) {
if sr < 0 || sr == (image.len() as i32) || sc < 0 || sc == (image[0].len() as i32) {
return;
}
let sr = sr as usize;
let sc = sc as usize;
if sr < 0 || image[sr][sc] == new_color || image[sr][sc] != target {
return;
}
image[sr][sc] = new_color;
let sr = sr as i32;
let sc = sc as i32;
Self::dfs(image, sr + 1, sc, new_color, target);
Self::dfs(image, sr - 1, sc, new_color, target);
Self::dfs(image, sr, sc + 1, new_color, target);
Self::dfs(image, sr, sc - 1, new_color, target);
}
pub fn flood_fill(image: Vec<Vec<i32>>, sr: i32, sc: i32, new_color: i32) -> Vec<Vec<i32>> {
let target = image[sr as usize][sc as usize];
Self::dfs(&mut image, sr, sc, new_color, target);
image
}
}