-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfloodFill.cpp
More file actions
36 lines (34 loc) · 1.08 KB
/
Copy pathfloodFill.cpp
File metadata and controls
36 lines (34 loc) · 1.08 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
#include <bits/stdc++.h>
using namespace std;
//using dfs
class Solution
{
public:
void dfs(int sr, int sc, int oldColor, int newColor, vector<vector<int>> &image, vector<vector<bool>> &visited)
{
int dr[] = {-1, 1, 0, 0};
int dc[] = {0, 0, -1, 1};
visited[sr][sc] = true;
image[sr][sc] = newColor;
for (int i = 0; i < 4; i++)
{
int r = sr + dr[i];
int c = sc + dc[i];
if (r < 0 || c < 0 || r >= image.size() || c >= image[0].size())
continue;
if (image[r][c] != oldColor)
continue;
if (!visited[r][c])
dfs(r, c, oldColor, newColor, image, visited);
}
}
vector<vector<int>> floodFill(vector<vector<int>> &image, int sr, int sc, int newColor)
{
vector<vector<bool>> visited(image.size(), vector<bool>(image[0].size(), 0));
int oldColor = image[sr][sc];
if (oldColor == newColor)
return image;
dfs(sr, sc, oldColor, newColor, image, visited);
return image;
}
};