-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflood_fill.cpp
More file actions
37 lines (29 loc) · 959 Bytes
/
flood_fill.cpp
File metadata and controls
37 lines (29 loc) · 959 Bytes
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
class Solution {
public:
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int color) {
int m = image.size();
int n = image[0].size();
int start_color = image[sr][sc];
if(start_color == color)
return image;
queue<pair<int, int>> q;
image[sr][sc] = color;
q.push({sr, sc});
const int dr[4] = {-1, 1, 0, 0};
const int dc[4] = {0, 0, -1, 1};
while(!q.empty()){
int r = q.front().first;
int c = q.front().second;
q.pop();
for(int i = 0; i < 4; i++){
int nr = r + dr[i];
int nc = c + dc[i];
if(nr < 0 || nr >= m || nc < 0 || nc >= n || image[nr][nc] != start_color)
continue;
image[nr][nc] = color;
q.push({nr, nc});
}
}
return image;
}
};