Browse Source

update: backtrack graph of klotski resolves

legacy
Dnomd343 2 years ago
parent
commit
b1abc5526b
  1. 2
      src/analyse/analyse.h
  2. 4
      src/analyse/backtrack.cc
  3. 26
      src/graph/graph.cc
  4. 28
      src/main.cc

2
src/analyse/analyse.h

@ -54,6 +54,6 @@ public:
typedef std::vector<std::unordered_map<uint64_t, track_t>> track_data_t;
// TODO: using RawCode instead of uint64_t
track_data_t backtrack(const std::vector<uint64_t> &codes);
track_data_t backtrack(const std::vector<RawCode> &codes);
};

4
src/analyse/backtrack.cc

@ -1,11 +1,11 @@
#include <algorithm>
#include "analyse.h"
Analyse::track_data_t Analyse::backtrack(const std::vector<uint64_t> &codes) {
Analyse::track_data_t Analyse::backtrack(const std::vector<RawCode> &codes) {
/// codes pre-check and sort by steps
std::vector<std::vector<analyse_t*>> 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
}

26
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<uint32_t> 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<uint32_t>(graph_layer[i].size()),
// .layer_num = i,
// .layer_index = static_cast<uint32_t>(graph_layer[i].size()),
.next = std::list<uint32_t>{},
}).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<uint32_t>(graph_layer[i - 1].size()),
.next = std::list<uint32_t>{curr->layer_index},
// .layer_num = i - 1,
// .layer_index = static_cast<uint32_t>(graph_layer[i - 1].size()),
// .next = std::list<uint32_t>{curr->layer_index},
.next = std::list<uint32_t>{layer_index},
}).first->second);
}

28
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();

Loading…
Cancel
Save