forked from Aadi71/Data-Structures-and-Algorithms-with-Aadi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flood-fill.cpp
34 lines (32 loc) · 1.05 KB
/
flood-fill.cpp
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
// https://leetcode.com/problems/flood-fill/
class Solution {
public:
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int color) {
if(image[sr][sc] == color) return image;
int temp = image[sr][sc];
int row = image.size();
int col = image[0].size();
queue<pair<int, int>> q;
q.push({sr, sc});
image[sr][sc] = color;
int a[] = {-1, 1, 0, 0};
int b[] = {0, 0, -1, 1};
while(!q.empty()){
int n = q.size();
while(n--){
int i = q.front().first;
int j = q.front().second;
q.pop();
for(int k = 0; k<4; k++){
int inew = i + a[k];
int jnew = j + b[k];
if(inew >= 0 && inew < row && jnew >= 0 && jnew < col && image[inew][jnew] == temp){
q.push({inew, jnew});
image[inew][jnew] = color;
}
}
}
}
return image;
}
};