-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay11.cpp
82 lines (42 loc) · 1.16 KB
/
Day11.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
72
73
74
75
76
77
78
79
80
81
82
// Basic Flood Fill Algorithm
class Solution {
public:
vector < vector <int> > floodFill( vector<vector<int>>& image, int sr, int sc, int newColor) {
vector <int> temp = image[0];
int row = image.size();
int col = temp.size();
//cout << row << " " << col << endl;
int dx[] = { 0, 0, +1, -1 };
int dy[] = { +1, -1, 0, 0 };
stack < pair < int, int > > st;
bool visit[55][55];
memset(visit, false, sizeof visit);
int cc = image[sr][sc];
st.push({ sr,sc });
visit[sr][sc] = true;
image[sr][sc] = newColor;
while (!st.empty()) {
int x1 = st.top().first;
int y1 = st.top().second;
st.pop();
for (int i = 0; i < 4; i++) {
int u = x1 + dx[i];
int v = y1 + dy[i];
if (u >= 0 and u < row and v >= 0 and v < col) {
if (visit[u][v] == false and image[u][v] == cc) {
st.push({ u,v });
image[u][v] = newColor;/// new color
visit[u][v] = true;
}
}
}
}
/*for (auto it : image) {
vector < int > tt = it;
for (auto x : tt)
cout << x << " ";
cout << endl;
}*/
return image;
}
};