forked from algorithm-archivists/algorithm-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flood_fill.cpp
156 lines (129 loc) · 3.87 KB
/
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <array>
#include <cassert>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
using CartesianIndex = std::array<int, 2>;
auto inbounds(CartesianIndex size, CartesianIndex loc) {
if (loc[0] < 0 || loc[1] < 0) {
return false;
} else if (loc[0] >= size[0] || loc[1] >= size[1]) {
return false;
}
return true;
}
auto find_neighbors(
std::vector<std::vector<float>> const& grid,
CartesianIndex loc,
float old_value,
float /* new_value */) {
const std::vector<CartesianIndex> possible_neighbors{
{loc[0], loc[1] + 1},
{loc[0] + 1, loc[1]},
{loc[0], loc[1] - 1},
{loc[0] - 1, loc[1]}};
std::vector<CartesianIndex> neighbors;
for (auto const& possible_neighbor : possible_neighbors) {
const auto size = CartesianIndex{
static_cast<int>(grid[0].size()), static_cast<int>(grid.size())};
const auto x = static_cast<std::size_t>(possible_neighbor[0]);
const auto y = static_cast<std::size_t>(possible_neighbor[1]);
if (inbounds(size, possible_neighbor) && grid[x][y] == old_value) {
neighbors.push_back(possible_neighbor);
}
}
return neighbors;
}
void recursive_fill(
std::vector<std::vector<float>>& grid,
CartesianIndex loc,
float old_value,
float new_value) {
if (old_value == new_value) {
return;
}
const auto x = static_cast<std::size_t>(loc[0]);
const auto y = static_cast<std::size_t>(loc[1]);
grid[x][y] = new_value;
const auto possible_neighbors = find_neighbors(grid, loc, old_value, new_value);
for (auto const& possible_neighbor : possible_neighbors) {
recursive_fill(grid, possible_neighbor, old_value, new_value);
}
}
void queue_fill(
std::vector<std::vector<float>>& grid,
CartesianIndex loc,
float old_value,
float new_value) {
if (old_value == new_value) {
return;
}
auto q = std::queue<CartesianIndex>{};
q.push(loc);
const auto x = static_cast<std::size_t>(loc[0]);
const auto y = static_cast<std::size_t>(loc[1]);
grid[x][y] = new_value;
while (q.size() > 0) {
const auto current_loc = q.front();
q.pop();
const auto possible_neighbors =
find_neighbors(grid, current_loc, old_value, new_value);
for (auto const& neighbor : possible_neighbors) {
const auto neighbor_x = static_cast<std::size_t>(neighbor[0]);
const auto neighbor_y = static_cast<std::size_t>(neighbor[1]);
grid[neighbor_x][neighbor_y] = new_value;
q.push(neighbor);
}
}
}
void stack_fill(
std::vector<std::vector<float>>& grid,
CartesianIndex loc,
float old_value,
float new_value) {
if (old_value == new_value) {
return;
}
auto s = std::stack<CartesianIndex>{};
s.push(loc);
while (s.size() > 0) {
const auto current_loc = s.top();
s.pop();
const auto x = static_cast<std::size_t>(current_loc[0]);
const auto y = static_cast<std::size_t>(current_loc[1]);
if (grid[x][y] == old_value) {
grid[x][y] = new_value;
const auto possible_neighbors =
find_neighbors(grid, current_loc, old_value, new_value);
for (auto const& neighbor : possible_neighbors) {
s.push(neighbor);
}
}
}
}
int main() {
const std::vector<std::vector<float>> grid{
{0, 0, 1, 0, 0},
{0, 0, 1, 0, 0},
{0, 0, 1, 0, 0},
{0, 0, 1, 0, 0},
{0, 0, 1, 0, 0}};
const std::vector<std::vector<float>> solution_grid{
{1, 1, 1, 0, 0},
{1, 1, 1, 0, 0},
{1, 1, 1, 0, 0},
{1, 1, 1, 0, 0},
{1, 1, 1, 0, 0}};
const CartesianIndex start_loc{1, 1};
auto test_grid = grid;
recursive_fill(test_grid, start_loc, 0.0, 1.0);
assert(test_grid == solution_grid);
test_grid = grid;
queue_fill(test_grid, start_loc, 0.0, 1.0);
assert(test_grid == solution_grid);
test_grid = grid;
stack_fill(test_grid, start_loc, 0.0, 1.0);
assert(test_grid == solution_grid);
return EXIT_SUCCESS;
}