-
Notifications
You must be signed in to change notification settings - Fork 0
/
Flood Fill
38 lines (33 loc) · 1.23 KB
/
Flood Fill
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
36
37
38
class Solution {
public:
void helper(vector<vector<int>>& image, int i , int j , int color , vector < vector <int> > &visited , int m , int n , int pix)
{
if( j >= image[0].size() || i >= image.size() )
{
return ;
}
image[i][j] = color ;
visited[i][j] = 1 ;
int a[4] = {1 , 0 , -1, 0} ;
int b[4] = {0 , 1, 0 , -1};
for(int k = 0 ; k < 4 ; k++)
{
int x = a[k] ;
int y = b[k] ;
// cout << i + x << j + y << endl ;
if(i + x < m && j + y < n && i + x >= 0 && j + y >= 0 && visited[i + x][j + y] == 0 && image[i + x][j + y] == pix)
{
// cout << "hey" << endl ;
helper(image , i + x ,j + y , color , visited , m , n , pix);
}
}
}
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int color) {
int m = image.size() ;
int n = image[0].size() ;
vector<vector<int>> visited ( m , vector <int>(n , 0) ) ;
int pix = image[sr][sc] ;
helper(image, sr , sc , color , visited , m , n, pix) ;
return image ;
}
};