From 4062d468c09356d678aaaa4c69e726632931e9d4 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Thu, 5 Jan 2023 23:15:07 +0800 Subject: [PATCH] update: AllCases class interface --- klotski/all_cases.cc | 24 ++++++++++++++++++++++++ klotski/all_cases.h | 10 ++++------ klotski/main.cc | 8 +++++--- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/klotski/all_cases.cc b/klotski/all_cases.cc index 3b2ee2f..238ae52 100644 --- a/klotski/all_cases.cc +++ b/klotski/all_cases.cc @@ -93,6 +93,30 @@ void AllCases::build_basic_ranges() { // build basic ranges } } +void AllCases::find_all_cases() { // find all valid cases + get_basic_ranges(); // ensure basic ranges exist + for (uint32_t head = 0; head < 16; ++head) { // address of 2x2 block + if ((head & 0b11) == 0b11) { + continue; // invalid 2x2 address + } + for (uint32_t range : basic_ranges) { // check base on 2x2 address and range + if (Common::check_case(head, range)) { + all_cases[head].emplace_back(Common::range_reverse(range)); // found valid case + } + } + } +} + const std::vector* AllCases::get_basic_ranges() { + if (basic_ranges.empty()) { + build_basic_ranges(); // basic ranges initialize + } return &basic_ranges; } + +const std::vector (*AllCases::get_all_cases())[16] { + if (all_cases->empty()) { + find_all_cases(); // all cases initialize + } + return &all_cases; +} diff --git a/klotski/all_cases.h b/klotski/all_cases.h index e2ada86..98084fc 100644 --- a/klotski/all_cases.h +++ b/klotski/all_cases.h @@ -6,16 +6,14 @@ class AllCases { public: // AllCases() {} -// void find_all_cases(); - - void build_basic_ranges(); - const std::vector* get_basic_ranges(); + const std::vector (*get_all_cases())[16]; private: -// std::vector all_cases[16]; std::vector basic_ranges; + std::vector all_cases[16]; -// void load_basic_ranges(); + void find_all_cases(); + void build_basic_ranges(); void generate_ranges(int n1, int n2, int n3, int n4); }; diff --git a/klotski/main.cc b/klotski/main.cc index 52e02e4..04f1d51 100644 --- a/klotski/main.cc +++ b/klotski/main.cc @@ -4,10 +4,12 @@ int main() { auto a = AllCases(); - a.build_basic_ranges(); - auto br = a.get_basic_ranges(); - std::cout << br->size() << std::endl; + std::cout << "basic range size: " << a.get_basic_ranges()->size() << std::endl; + + for (const auto &temp : *a.get_all_cases()) { + std::cout << temp.size() << std::endl; + } return 0; }