Browse Source

update: backtrack functions for FastCal

master
Dnomd343 2 years ago
parent
commit
d512fd0368
  1. 53
      src/fast_cal/fast_cal.cc
  2. 10
      src/fast_cal/fast_cal.h
  3. 11
      src/main.cc

53
src/fast_cal/fast_cal.cc

@ -1,12 +1,10 @@
#include <algorithm>
#include "core.h"
#include "common.h"
#include "fast_cal.h"
#include "raw_code.h"
#include <iostream>
#include <algorithm>
/// 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<RawCode> FastCal::solve_multi(RawCode code) {
});
}
std::vector<uint64_t> FastCal::backtrack(uint64_t code) {
std::vector<uint64_t> 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<RawCode> FastCal::backtrack(RawCode code) {
auto tmp = cases.find((uint64_t)code);
if (tmp == cases.end()) {
return std::vector<RawCode>{}; // code not exist
}
auto node = &tmp->second; // backtrack entry
std::vector<RawCode> 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;
}

10
src/fast_cal/fast_cal.h

@ -15,7 +15,9 @@ class FastCal {
public:
typedef std::function<bool(uint64_t)> match_t;
/// xxx_multi only search until same layer
/// backtrack functions
int step_num(RawCode code);
std::vector<RawCode> backtrack(RawCode code);
/// BFS search functions
RawCode solve(RawCode code);
@ -25,15 +27,11 @@ public:
std::vector<RawCode> 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<uint64_t> backtrack(uint64_t code);
uint32_t step_num(uint64_t code);
// TODO: static search functions
private:

11
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;

Loading…
Cancel
Save