From b1abc5526bf20d42d9a4e1b66dd8709aaf8fb45f Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sat, 21 Jan 2023 15:13:40 +0800 Subject: [PATCH] update: backtrack graph of klotski resolves --- src/analyse/analyse.h | 2 +- src/analyse/backtrack.cc | 4 ++-- src/graph/graph.cc | 26 ++++++++++++++++---------- src/main.cc | 28 +++++++++++++++++++--------- 4 files changed, 38 insertions(+), 22 deletions(-) diff --git a/src/analyse/analyse.h b/src/analyse/analyse.h index c57d9a5..406c733 100644 --- a/src/analyse/analyse.h +++ b/src/analyse/analyse.h @@ -54,6 +54,6 @@ public: typedef std::vector> track_data_t; // TODO: using RawCode instead of uint64_t - track_data_t backtrack(const std::vector &codes); + track_data_t backtrack(const std::vector &codes); }; diff --git a/src/analyse/backtrack.cc b/src/analyse/backtrack.cc index a755696..e5f1b00 100644 --- a/src/analyse/backtrack.cc +++ b/src/analyse/backtrack.cc @@ -1,11 +1,11 @@ #include #include "analyse.h" -Analyse::track_data_t Analyse::backtrack(const std::vector &codes) { +Analyse::track_data_t Analyse::backtrack(const std::vector &codes) { /// codes pre-check and sort by steps std::vector> todos; for (const auto &code : codes) { - auto c = cases.find(code); + auto c = cases.find((uint64_t)code); if (c == cases.end()) { // invalid input return track_data_t{}; // return empty data } diff --git a/src/graph/graph.cc b/src/graph/graph.cc index de3fc36..2c03790 100644 --- a/src/graph/graph.cc +++ b/src/graph/graph.cc @@ -30,14 +30,15 @@ void Graph::svg_demo(Analyse::track_data_t track_data) { // // } + auto skin = CaseSkin(); // TODO: remove layer_index // TODO: try remove layer_num struct graph_t { uint64_t code; - uint32_t layer_num; - uint32_t layer_index; +// uint32_t layer_num; +// uint32_t layer_index; std::list next; }; @@ -52,8 +53,8 @@ void Graph::svg_demo(Analyse::track_data_t track_data) { graph_layer[i].emplace_back(&graph_data.emplace(c.second.code, graph_t { .code = c.second.code, - .layer_num = i, - .layer_index = static_cast(graph_layer[i].size()), +// .layer_num = i, +// .layer_index = static_cast(graph_layer[i].size()), .next = std::list{}, }).first->second); @@ -61,14 +62,17 @@ void Graph::svg_demo(Analyse::track_data_t track_data) { } } - for (uint32_t i = 81; i > 0; --i) { + for (uint32_t i = graph_layer.size() - 1; i > 0; --i) { // std::cout << std::endl; // std::cout << "layer " << i << " -> " << graph_layer[i].size() << std::endl; - for (const auto *curr : graph_layer[i]) { +// for (const auto *curr : graph_layer[i]) { + for (uint32_t layer_index = 0; layer_index < graph_layer[i].size(); ++layer_index) { // std::cout << "code = " << c->code << std::endl; + const auto *curr = graph_layer[i][layer_index]; + for (const auto *src : track_data[i][curr->code].last) { // std::cout << " src -> " << src->code << std::endl; @@ -76,14 +80,16 @@ void Graph::svg_demo(Analyse::track_data_t track_data) { if (find_ret != graph_data.end()) { // already exist - find_ret->second.next.emplace_back(curr->layer_index); +// find_ret->second.next.emplace_back(curr->layer_index); + find_ret->second.next.emplace_back(layer_index); } else { graph_layer[i - 1].emplace_back(&graph_data.emplace(src->code, graph_t { .code = src->code, - .layer_num = i - 1, - .layer_index = static_cast(graph_layer[i - 1].size()), - .next = std::list{curr->layer_index}, +// .layer_num = i - 1, +// .layer_index = static_cast(graph_layer[i - 1].size()), +// .next = std::list{curr->layer_index}, + .next = std::list{layer_index}, }).first->second); } diff --git a/src/main.cc b/src/main.cc index 2048c39..11b85ff 100644 --- a/src/main.cc +++ b/src/main.cc @@ -57,20 +57,30 @@ int main() { // } - auto a = Analyse(RawCode::from_common_code("1a9bf0c")); - a.build(); -// auto ret = a.build_until([](uint64_t code) { -// return ((code >> (3 * 0xD)) & 0b111) == B_2x2; -// }); +// auto a = Analyse(RawCode::from_common_code("1a9bf0c")); +// auto a = Analyse(RawCode::from_common_code("4fea134")); +// auto a = Analyse(RawCode::from_common_code("A5D3AF")); + auto a = Analyse(RawCode::from_common_code("1FA6F80")); +// a.build(); + auto ret = a.build_until([](uint64_t code) { + return ((code >> (3 * 0xD)) & 0b111) == B_2x2; + }); + +// std::cout << "analyse complete" << std::endl; // for (const auto &r : ret) { // std::cout << r << std::endl; // } - auto ret = a.backtrack({ - 0x7F87E0E5BFFF492, 0x1FB1E36F9FFF492 - }); +// auto ret = a.backtrack({ +// 0x7F87E0E5BFFF492, 0x1FB1E36F9FFF492, RawCode::from_common_code("2CEA8DC").unwrap() +// }); + + auto svg_ret = a.backtrack(ret); + +// std::cout << "backtrack complete" << std::endl; + auto g = Graph(); - g.svg_demo(ret); + g.svg_demo(svg_ret); // auto start_time = clock();