いきなり必要になった画像の領域塗りつぶしのアルゴリズム。
stack じゃなくて queue でも良いはず。
最初再帰で処理した名残。再帰で処理すると stack over flow で死んだ。
void fill(int x, int y) {
struct POINT {
int x;
int y;
};
std::stack<POINT> stack;
stack.push(POINT({x, y}));
while (stack.empty() == false) {
auto point = stack.top();
stack.pop();
// すでに塗りつぶされている点であればスキップ
if (isAlreadyProecssed(point)) {
continue;
}
// 塗りつぶしが必要か判定
if (needProcess(point) == false) {
continue;
}
// 塗りつぶし
process(point);
stack.push(POINT({point.x - 1, point.y}));
stack.push(POINT({point.x + 1, point.y}));
stack.push(POINT({point.x, point.y - 1}));
stack.push(POINT({point.x, point.y + 1}));
}
}