From d512fd036878e28554a3625f4fea112b40928fd5 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Thu, 19 Jan 2023 14:44:55 +0800 Subject: [PATCH] update: backtrack functions for FastCal --- src/fast_cal/fast_cal.cc | 53 ++++++++++++++++++---------------------- src/fast_cal/fast_cal.h | 10 +++----- src/main.cc | 11 ++++++++- 3 files changed, 38 insertions(+), 36 deletions(-) diff --git a/src/fast_cal/fast_cal.cc b/src/fast_cal/fast_cal.cc index 7a0d180..42c7b8f 100644 --- a/src/fast_cal/fast_cal.cc +++ b/src/fast_cal/fast_cal.cc @@ -1,12 +1,10 @@ +#include #include "core.h" #include "common.h" #include "fast_cal.h" - #include "raw_code.h" -#include -#include - +/// klotski resolved -> 2x2 block at address 13 (aka 0xD) RawCode FastCal::solve(RawCode code) { return FastCal::target(code, [](uint64_t code) { return ((code >> (3 * 0xD)) & 0b111) == B_2x2; // check 2x2 block address @@ -19,34 +17,31 @@ std::vector FastCal::solve_multi(RawCode code) { }); } -std::vector FastCal::backtrack(uint64_t code) { - std::vector path; - - // TODO: confirm code exist - auto node = &cases[code]; - - while (node != nullptr) { - path.emplace_back(node->code); - node = node->last; +/// backtrack of FastCal tree +int FastCal::step_num(RawCode code) { + auto tmp = cases.find((uint64_t)code); + if (tmp == cases.end()) { + return -1; // code not exist } - - std::reverse(path.begin(), path.end()); - return path; - -} - - - -uint32_t FastCal::step_num(uint64_t code) { - - uint32_t num = 0; - - // TODO: confirm code exist - auto node = &cases[code]; - + int num = 0; // step number + auto node = &tmp->second; // backtrack entry while ((node = node->last) != nullptr) { ++num; } - return num; } + +std::vector FastCal::backtrack(RawCode code) { + auto tmp = cases.find((uint64_t)code); + if (tmp == cases.end()) { + return std::vector{}; // code not exist + } + auto node = &tmp->second; // backtrack entry + std::vector path; // backtrack path + while (node != nullptr) { + path.emplace_back(RawCode::unsafe_create(node->code)); // record path info + node = node->last; + } + std::reverse(path.begin(), path.end()); // reverse path cases + return path; +} diff --git a/src/fast_cal/fast_cal.h b/src/fast_cal/fast_cal.h index 50e72e7..395745b 100644 --- a/src/fast_cal/fast_cal.h +++ b/src/fast_cal/fast_cal.h @@ -15,7 +15,9 @@ class FastCal { public: typedef std::function match_t; - /// xxx_multi only search until same layer + /// backtrack functions + int step_num(RawCode code); + std::vector backtrack(RawCode code); /// BFS search functions RawCode solve(RawCode code); @@ -25,15 +27,11 @@ public: std::vector target_multi(RawCode code, const match_t &match); + // TODO: static search functions // TODO: search / search_multi / resolve / resolve_multi // TODO: static furthest function - std::vector backtrack(uint64_t code); - - uint32_t step_num(uint64_t code); - - // TODO: static search functions private: diff --git a/src/main.cc b/src/main.cc index 0b1dad2..6eaf102 100644 --- a/src/main.cc +++ b/src/main.cc @@ -28,12 +28,21 @@ int main() { auto f = FastCal(); // auto ret = f.solve((uint64_t)RawCode::from_common_code("1a9bf0c")); auto ret = f.solve(RawCode::from_common_code("1a9bf0c")); + +// auto ret = f.target(RawCode::from_common_code("1a9bf0c"), [](uint64_t code) { +// return code == (uint64_t)RawCode::from_common_code(0xDB23B6C00); +// return code == 0x0FFB5E7E5AEC741A; +// }); + // auto ret = f.solve(RawCode::from_common_code("1aaef0c")); if (ret == FC_NOT_FOUND) { std::cout << "not found" << std::endl; } else { - std::cout << ret << std::endl; + for (const auto &r : f.backtrack(ret)) { + std::cout << r << std::endl; + } + std::cout << "step number: " << f.step_num(ret) << std::endl; } // std::cout << f.step_num(ret) << std::endl;