Flood-Fill Algorithm, Also Called Seed Fill Algorithm
Flood-Fill Algorithm, Also Called Seed Fill Algorithm
#include <iostream>
#include <stack>
return (x >= 0) && (x < ROW) && (y >= 0) && (y < COL) && (image[x][y] == target_color);
st.push({x, y});
while (!st.empty()) {
auto p = st.top();
st.pop();
int x = p.first;
int y = p.second;
image[x][y] = fill_color;
st.push({x+1, y});
st.push({x-1, y});
st.push({x, y+1});
st.push({x, y-1});
int main() {
int image[ROW][COL] = {
{1, 1, 1, 1, 1},
{1, 1, 1, 1, 1},
{1, 1, 2, 2, 2},
{1, 1, 2, 2, 2},
{1, 1, 2, 2, 2}
};
int x = 2, y = 2, fill_color = 3;
return 0;
The boundary-fill algorithm is similar to the flood-fill algorithm, but it fills an area only if the
color of the seed point is different from the target color and the fill color.
#include <iostream>
#include <stack>
return (x >= 0) && (x < ROW) && (y >= 0) && (y < COL) && (image[x][y] != fill_color) && (image[x][y]
== target_color);
st.push({x, y});
while (!st.empty()) {
auto p = st.top();
st.pop();
int x = p.first;
int y = p.second;
continue;
}
image[x][y] = fill_color;
st.push({x+1, y});
st.push({x-1, y});
st.push({x, y+1});
st.push({x, y-1});
int main() {
int image[ROW][COL] = {
{1, 1, 1, 1, 1},
{1, 2, 2, 2, 1},
{1, 2, 2, 2, 1},
{1, 2, 2, 2, 1},
{1, 1, 1, 1, 1}
};
int x = 2, y = 2, fill_color = 3;
return 0;