forked from Nimesh-Srivastava/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0733.cpp
More file actions
35 lines (26 loc) · 973 Bytes
/
0733.cpp
File metadata and controls
35 lines (26 loc) · 973 Bytes
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
class Solution {
public:
void dfs(vector<vector<int>>& image, int i, int j, int newColor, int old){
//base conditions
if(i < 0 || j < 0
|| i > image.size() - 1
|| j > image[0].size() - 1
|| image[i][j] != old)
return;
//define visited cell
image[i][j] = newColor;
cout<<"image["<<i<<"]["<<j<<"] : "<<image[i][j]<<endl;
//dfs calls
dfs(image, i - 1, j, newColor, old); //top
dfs(image, i + 1, j, newColor, old); //bottom
dfs(image, i, j - 1, newColor, old); //left
dfs(image, i, j + 1, newColor, old); //right
}
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
int old = image[sr][sc];
if(newColor == old)
return image;
dfs(image, sr, sc, newColor, old);
return image;
}
};