diff --git a/src/core/fast_cal/fast_cal.h b/src/core/fast_cal/fast_cal.h index 5b7aef8..c933057 100644 --- a/src/core/fast_cal/fast_cal.h +++ b/src/core/fast_cal/fast_cal.h @@ -131,6 +131,11 @@ public: // ------------------------------------------------------------------------------------- // + /// Backtrack the shortest path from specified case. + std::vector backtrack(RawCode code); + + // ------------------------------------------------------------------------------------- // + private: // ------------------------------------------------------------------------------------- // diff --git a/src/core/fast_cal/internal/demo.cc b/src/core/fast_cal/internal/demo.cc index f1dacf3..ae9e61e 100644 --- a/src/core/fast_cal/internal/demo.cc +++ b/src/core/fast_cal/internal/demo.cc @@ -96,13 +96,29 @@ std::vector FastCalPro::furthest() { } } +std::vector FastCalPro::backtrack(RawCode code) { + if (const auto match = cases_.find(code.unwrap()); match == cases_.end()) { + return {}; // case not found + } + std::vector path; + while (code != 0) { + path.emplace_back(code); + code = RawCode::unsafe_create(cases_.find(code.unwrap())->second.back); + } + std::reverse(path.begin(), path.end()); + return path; +} + RawCode FastCal_demo(RawCode raw_code) { klotski::fast_cal::FastCalPro fc {raw_code}; - return fc.solve().value(); +// return fc.solve().value(); -// auto tmp = fc.solve(); + auto tmp = fc.solve(); // std::cout << tmp.value().to_common_code() << std::endl; + auto path = fc.backtrack(tmp.value()); + std::cout << path.size() << std::endl; + // auto tmp = fc.solve_multi(); // for (const auto x : tmp) { // std::cout << x.to_common_code() << std::endl; @@ -118,5 +134,5 @@ RawCode FastCal_demo(RawCode raw_code) { // }); // std::cout << tmp.value().to_common_code() << std::endl; -// return RawCode::unsafe_create(0); + return RawCode::unsafe_create(0); }