-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflood_fill.cpp
More file actions
23 lines (20 loc) · 798 Bytes
/
flood_fill.cpp
File metadata and controls
23 lines (20 loc) · 798 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public:
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
if(image[sr][sc] == newColor) return image;
fill(image, sr, sc, newColor, image[sr][sc]);
return image;
}
void fill(vector<vector<int>>& image, int sr, int sc, int newColor, int oldColor) {
int n = image.size();
int m = image[0].size();
if(sr < 0 || sc < 0 || sr >= n || sc >= m || oldColor != image[sr][sc]) {
return ;
}
image[sr][sc] = newColor;
fill(image, sr + 1, sc, newColor, oldColor);
fill(image, sr - 1, sc, newColor, oldColor);
fill(image, sr, sc + 1, newColor, oldColor);
fill(image, sr, sc - 1, newColor, oldColor);
}
};