-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDFS_Flood_Fill.cpp
71 lines (62 loc) · 2.12 KB
/
DFS_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
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
65
66
67
68
69
70
71
// Flood fill using depth first search
// Given an image, the starting pixel and the new color, fill the image with the new color if
// the 4 directional pixels are of same color as the starting pixel and,
// their 4 directional pixels are of same color as the starting pixel and so on ...
// Example input -
// [[1,1,1],
// [1,1,0],
// [1,0,1]]
// sr = 1, sc = 1, newColor = 2
// Expected output -
// [[2,2,2],
// [2,2,0],
// [2,0,1]]
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor)
{
int H = image.size();
int W = image[0].size();
int col = image[sr][sc];
image[sr][sc] = newColor;
std::queue<pair<int,int>> fq;
fq.push({sr,sc});
vector<pair<int,int>> directions {{1,0},{0,1},{-1,0},{0,-1}};
vector<vector<bool>> vis(H, vector<bool>(W));
auto inside = [&](int i, int j){
return (0 <= i && i< H && 0 <= j && j < W); };
while(!fq.empty())
{
pair<int,int> fpair = fq.front();
fq.pop();
vis[fpair.first][fpair.second] = true;
for(pair<int,int> dir : directions)
{
int new_sr = fpair.first + dir.first;
int new_sc = fpair.second + dir.second;
if(inside(new_sr,new_sc) && !vis[new_sr][new_sc] && image[new_sr][new_sc] == col)
{
image[new_sr][new_sc] = newColor;
fq.push({new_sr,new_sc});
vis[new_sr][new_sc] = true;
}
}
}
return image;
}
int main()
{
std::vector<std::vector<int>> fvec {{1,1,1},{1,1,0},{1,0,1}};
floodFill(fvec);
for(auto& vec : fvec)
{
for(int& num : vec)
{
std::cout << num << ",";
}
std::cout << "\n" << std::endl;
}
return 0;
}