1 impl Solution { 2 pub fn flood_fill(image: Vec<Vec<i32>>, sr: i32, sc: i32, new_color: i32) -> Vec<Vec<i32>> { 3 let mut image = image; 4 let origin_Color = image[sr as usize][sc as usize].clone(); 5 Self::dfs(&mut image, sr, sc, origin_Color, new_color); 6 image 7 } 8 fn dfs(image: &mut Vec<Vec<i32>>, sr:i32, sc:i32, origin_Color:i32, new_color:i32) { 9 if sr < 0 || sc < 0 || sr >= image.len() as i32 || sc >= image[0].len() as i32 || image[sr as usize][sc as usize] == new_color || image[sr as usize][sc as usize] != origin_Color { 10 return; 11 } 12 image[sr as usize][sc as usize] = new_color; 13 Self::dfs(image, sr-1, sc, origin_Color, new_color); 14 Self::dfs(image, sr+1, sc, origin_Color, new_color); 15 Self::dfs(image, sr, sc-1, origin_Color, new_color); 16 Self::dfs(image, sr, sc+1, origin_Color, new_color); 17 } 18 }
来源:https://www.cnblogs.com/chenguifeng/p/12366980.html