A slightly working flood iterator and a working compass iterator.
This commit is contained in:
parent
043c0d91df
commit
1295e9631d
6 changed files with 214 additions and 4 deletions
85
matrix.cpp
85
matrix.cpp
|
@ -1,5 +1,6 @@
|
|||
#include "matrix.hpp"
|
||||
#include "constants.hpp"
|
||||
#include "dbc.hpp"
|
||||
#include <fmt/core.h>
|
||||
|
||||
using namespace fmt;
|
||||
|
@ -83,6 +84,89 @@ namespace matrix {
|
|||
x, y, left, right, top, bottom);
|
||||
}
|
||||
|
||||
compass::compass(Matrix &mat, size_t x, size_t y) :
|
||||
x(x), y(y)
|
||||
{
|
||||
array<int, 4> x_in{0, 1, 0, -1};
|
||||
array<int, 4> y_in{-1, 0, 1, 0};
|
||||
|
||||
for(size_t i = 0; i < 4; i++) {
|
||||
int nx = x + x_in[i];
|
||||
int ny = y + y_in[i];
|
||||
if(matrix::inbounds(mat, nx, ny)) {
|
||||
x_dirs[max_dirs] = nx;
|
||||
y_dirs[max_dirs] = ny;
|
||||
max_dirs++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool compass::next() {
|
||||
dir++;
|
||||
if(dir < max_dirs) {
|
||||
x = x_dirs[dir];
|
||||
y = y_dirs[dir];
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
flood::flood(Matrix &mat, Point start, int old_val, int new_val) :
|
||||
mat(mat), start(start), old_val(old_val), new_val(new_val),
|
||||
dirs{mat, start.x, start.y}
|
||||
{
|
||||
dbc::check(old_val != new_val, "what you doing?");
|
||||
current_loc = start;
|
||||
q.push(start);
|
||||
}
|
||||
|
||||
bool flood::next() {
|
||||
if(!q.empty()) {
|
||||
if(!dirs.next()) {
|
||||
// box is done reset it
|
||||
auto current_loc = q.front();
|
||||
q.pop();
|
||||
|
||||
dirs = matrix::compass{mat, current_loc.x, current_loc.y};
|
||||
dirs.next();
|
||||
}
|
||||
|
||||
// get the next thing
|
||||
if(mat[dirs.y][dirs.x] == old_val) {
|
||||
mat[dirs.y][dirs.x] += new_val;
|
||||
x = dirs.x;
|
||||
y = dirs.y;
|
||||
|
||||
q.push({.x=dirs.x, .y=dirs.y});
|
||||
}
|
||||
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool flood::next_working() {
|
||||
if(!q.empty()) {
|
||||
auto current_loc = q.front();
|
||||
q.pop();
|
||||
|
||||
for(matrix::in_box box{mat, current_loc.x, current_loc.y, 1};
|
||||
box.next();)
|
||||
{
|
||||
if(mat[box.y][box.x] == old_val) {
|
||||
mat[box.y][box.x] += new_val;
|
||||
q.push({.x=box.x, .y=box.y});
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void dump(const std::string &msg, Matrix &map, int show_x, int show_y) {
|
||||
println("----------------- {}", msg);
|
||||
|
||||
|
@ -102,4 +186,5 @@ namespace matrix {
|
|||
if(it.row) print("\n");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue