-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfloodfill_bfs.cpp
38 lines (30 loc) · 1.17 KB
/
floodfill_bfs.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
35
36
37
38
class Solution {
private:
vector<pair<int, int>> directions = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
bool isValidCell(int row, int col, int m, int n, vector<vector<int>> &image, int oldColor) {
if(row >= 0 and row < m and col >= 0 and col < n and image[row][col] == oldColor) return true;
else return false;
}
public:
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
if(image.size() == 0) return vector<vector<int>>();
int m = image.size();
int n = image[0].size();
int oldColor = image[sr][sc];
if(oldColor == newColor) return image;
queue<pair<int, int>> Q;
Q.push({sr, sc});
while(!Q.empty()) {
auto &[r, c] = Q.front();
Q.pop();
image[r][c] = newColor;
for(int i=0; i<4; i++) {
int x = r + directions[i].first, y = c + directions[i].second;
if(isValidCell(x, y, m, n, image, oldColor)) {
Q.push({x, y});
}
}
}
return image;
}
};