This commit is contained in:
2022-11-20 13:52:18 -08:00
parent 234165c7f3
commit 6443b64932
8 changed files with 271 additions and 54 deletions

View File

@@ -228,43 +228,42 @@ auto Dlx::RemoveRow(std::size_t i) -> void {
}
auto Dlx::Solve(
std::function<void(std::size_t, std::size_t, std::size_t)> try_cb,
std::function<void()> undo_cb,
std::function<void()> found_cb,
std::function<void(std::size_t)> stuck_cb) -> void
std::function<bool(std::size_t, std::size_t, std::size_t)> try_cb,
std::function<bool()> undo_cb,
std::function<bool()> found_cb,
std::function<bool(std::size_t)> stuck_cb) -> void
{
auto const recurse = [&](auto const& self) -> void {
auto const recurse = [&](auto const& self) -> bool {
auto c = root_->R;
if (c == root_) {
found_cb();
return;
return found_cb();
}
auto s = std::numeric_limits<std::size_t>::max();
auto s = c->s;
for (auto const i : root_->rightwards()) {
if (i->s < s) {
s = (c = i)->s;
if (s == 0) {
stuck_cb(c->n);
return;
}
}
}
if (s == 0) {
return stuck_cb(c->n);
}
c->CoverCol();
for (auto const r : c->downwards()) {
try_cb(c->n, s, r->n);
if (try_cb(c->n, s, r->n)) [[unlikely]] return true;
for (auto const j : r->rightwards()) {
j->c->CoverCol();
}
self(self);
undo_cb();
if (self(self)) [[unlikely]] return true;
if (undo_cb()) [[unlikely]] return true;
for (auto const j : r->leftwards()) {
j->c->UncoverCol();
}
}
c->UncoverCol();
return false;
};
recurse(recurse);
}