Skip to content

Commit a687594

Browse files
authored
Create 733. Flood Fill
1 parent b05271a commit a687594

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

Graph/733. Flood Fill

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution {
2+
private:
3+
void dfs(int row , int col , int color , int initColor ,
4+
vector<vector<int>>& image , vector<vector<int>>&ans)
5+
{
6+
//color with newColor
7+
ans[row][col] = color ;
8+
int n = image.size();
9+
int m = image[0].size();
10+
11+
//check for valid cordintes
12+
for(int delRow = -1 ; delRow <= 1 ; delRow++)
13+
{
14+
for(int delCol = -1 ; delCol <= 1 ; delCol++)
15+
{
16+
if(abs(delRow) == abs(delCol)) {continue;}
17+
int nRow = row + delRow;
18+
int nCol = col + delCol;
19+
// check for valid coordinate
20+
// then check for same initial color and unvisited pixel
21+
if(nRow >= 0 && nRow < n && nCol >= 0 && nCol < m &&
22+
image[nRow][nCol] == initColor && ans[nRow][nCol] != color ){
23+
dfs(nRow , nCol , color , initColor , image , ans);
24+
}
25+
}
26+
}
27+
}
28+
public:
29+
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int color) {
30+
// get initial color
31+
int initColor = image[sr][sc];
32+
vector<vector<int>> ans = image ;
33+
34+
dfs(sr , sc , color , initColor , image , ans);
35+
return ans ;
36+
}
37+
};

0 commit comments

Comments
 (0)