-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFlood Fill.cpp
More file actions
64 lines (58 loc) · 2.13 KB
/
Copy pathFlood Fill.cpp
File metadata and controls
64 lines (58 loc) · 2.13 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
class Solution {// DFS SOLUTION
private:
void dfs(vector<vector<int>>&image ,vector<vector<int>>&temp, vector<vector<int>>&vis , int row ,int col, int color ,int original,int m ,int n){
vis[row][col] = 1 ;
temp[row][col] = color;
int delrow[] = {-1,0,+1,0};
int delcol[] = {0,+1,0,-1};
for(int i=0; i<4; i++){
int nrow = delrow[i] + row;
int ncol = delcol[i] + col;
if(nrow >= 0 and nrow < m and ncol >= 0 and ncol < n and image[nrow][ncol] == original and !vis[nrow][ncol]){
dfs(image,temp,vis,nrow,ncol,color,original,m,n);
}
}
}
public:
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>>vis(m,vector<int>(n,0)) ;
vector<vector<int>>temp = image;
int original = image[sr][sc];
dfs(image,temp,vis,sr,sc,color,original,m,n);
return temp ;
}
};/// BFS SOLUTION
class Solution {
public:
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int color) {
int m = image.size();
if (m == 0) return image;
int n = image[0].size();
int original = image[sr][sc];
if (original == color) return image;
vector<vector<int>> temp = image;
queue<pair<int, int>> q;
q.push({sr, sc});
temp[sr][sc] = color;
int delrow[] = {-1, 0, 1, 0};
int delcol[] = {0, 1, 0, -1};
while (!q.empty()) {
pair<int, int> current = q.front();
q.pop();
int row = current.first;
int col = current.second;
for (int i = 0; i < 4;i++) {
int nrow = row + delrow[i];
int ncol = col + delcol[i];
if (nrow >= 0 && nrow < m && ncol >= 0 && ncol < n &&
image[nrow][ncol] == original && temp[nrow][ncol] != color) {
q.push({nrow, ncol});
temp[nrow][ncol] = color;
}
}
}
return temp;
}
};