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

@@ -4,6 +4,7 @@
#include <cstddef>
#include <functional>
#include <vector>
#include <utility>
namespace dlx {
@@ -39,8 +40,8 @@ public:
// Increases the number of rows and columns if necessary.
auto Set(std::size_t row, std::size_t col) -> void;
// Marks a column as optional: a solution need not cover the given column,
// but it still must respect the constraints it entails.
// Marks a column as optional: a solution need not cover the given column,
// but it still must respect the constraints it entails.
auto MarkOptional(std::size_t col) -> void;
// Removes a row from consideration.
@@ -53,19 +54,19 @@ public:
auto PickRow(std::size_t row) -> void;
auto 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 ForallCover(Dlx& dlx, auto cb) {
std::vector<std::size_t> sol;
dlx.Solve(
[&](std::size_t c, std::size_t s, std::size_t r) { sol.push_back(r); },
[&](){ sol.pop_back(); },
[&](){ cb(sol); },
[](std::size_t){}
[&](std::size_t c, std::size_t s, std::size_t r) { sol.push_back(r); return false; },
[&](){ sol.pop_back(); return false; },
[&](){ return cb(std::as_const(sol)); },
[](std::size_t){ return false; }
);
}