forked from VibhutiJindal/CodeChef
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FloodFill
23 lines (23 loc) · 844 Bytes
/
FloodFill
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 {
void dfs(vector<vector<int>> &image,int sr,int sc,int newColor, int rows,int cols,int source){
if(sr<0 || sr>=rows || sc<0 ||sc>=cols)
return;
else if(image[sr][sc]!=source)
return;
image[sr][sc]=newColor;
dfs(image,sr-1,sc,newColor,rows,cols,source);
dfs(image,sr+1,sc,newColor,rows,cols,source);
dfs(image,sr,sc-1,newColor,rows,cols,source);
dfs(image,sr,sc+1,newColor,rows,cols,source);
}
public:
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
if(newColor==image[sr][sc])
return image;
int rows=image.size();
int cols=image[0].size();
int source=image[sr][sc];
dfs(image,sr,sc,newColor,rows,cols,source);
return image;
}
};