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