From aa4dc085e912e64118e70b0a71d9432d31ff66e6 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Tue, 11 Apr 2023 14:13:27 +0800 Subject: [PATCH] update: build all groups --- src/klotski_core/ffi/tmain.cc | 7 ++- src/klotski_core/group/group.cc | 93 ++++++--------------------------- 2 files changed, 22 insertions(+), 78 deletions(-) diff --git a/src/klotski_core/ffi/tmain.cc b/src/klotski_core/ffi/tmain.cc index 4a895b5..63a4fa5 100644 --- a/src/klotski_core/ffi/tmain.cc +++ b/src/klotski_core/ffi/tmain.cc @@ -31,7 +31,12 @@ void tmain() { // auto ret = Group::build_groups(0); // std::cout << ret[0].size() << std::endl; - Group::build_groups(123); +// auto g = Group::build_groups(123); +// std::cout << g.size() << std::endl; + + for (uint32_t type_id = 0; type_id < 204; ++type_id) { + std::cout << Group::build_groups(type_id).size() << std::endl; + } // printf("tmain exit\n"); } diff --git a/src/klotski_core/group/group.cc b/src/klotski_core/group/group.cc index eaa7e35..b72c504 100644 --- a/src/klotski_core/group/group.cc +++ b/src/klotski_core/group/group.cc @@ -80,98 +80,37 @@ std::vector Group::group_cases(const RawCode &seed) { std::vector> Group::build_groups(uint32_t type_id) { auto all_cases = Group::all_cases(type_id); - if (all_cases.empty()) { // match type_id TODO: ??? (7-0-0) - return {}; + if (all_cases.empty()) { + return {}; // type 203 -> no element } std::vector> groups; - auto min = std::min_element(all_cases.begin(), all_cases.end()); - auto first_group = Group::group_cases(min->to_raw_code()); + auto min = std::min_element(all_cases.begin(), all_cases.end()); // search min CommonCode + auto first_group = Group::group_cases(min->to_raw_code()); // expand the first group groups.emplace_back(first_group.begin(), first_group.end()); if (first_group.size() == all_cases.size()) { // only contains one group - -// std::vector rs(k.begin(), k.end()); -// std::vector rs; -// rs.reserve(k.size()); - -// for (auto &&rr : k) { -// -// std::cout << rr.to_common_code() << std::endl; - -// rs.emplace_back(rr.to_common_code()); -// } - -// std::sort(rs.begin(), rs.end()); -// -// for (auto &&rr : rs) { -// std::cout << rr << std::endl; -// } - -// std::cout << r[0] << std::endl; - -// std::cout << k[0].to_common_code() << std::endl; - -// return - return groups; } - - // TODO: reuse first_group - - - std::set cases(all_cases.begin(), all_cases.end()); - for (auto &&tmp : groups[0]) { - -// auto c = tmp.to_common_code(); - - cases.erase(tmp); - -// current_group->emplace_back(c); - + cases.erase(tmp); // remove elements in first group } - - -// std::set cases; - -// uint32_t index = 0; -// for (auto &&tmp : all_cases) { -// if -// } - -// std::cout << cases.size() << std::endl; -// std::cout << all_cases.size() << std::endl; - - while (!cases.empty()) { - auto ret = Group::group_cases(cases.begin()->to_raw_code()); - - groups.emplace_back(); - - auto current_group = groups.end() - 1; - - for (auto &&tmp : ret) { - - auto c = tmp.to_common_code(); - - cases.erase(c); - - current_group->emplace_back(c); - + groups.emplace_back(); // create empty vector + auto current_group = groups.end() - 1; // insert into latest + for (auto &&tmp : Group::group_cases(cases.begin()->to_raw_code())) { + auto common_code = tmp.to_common_code(); + current_group->emplace_back(common_code); // insert into current group + cases.erase(common_code); // remove from global union } - } - std::stable_sort(groups.begin(), groups.end(), [](const std::vector &c1, const std::vector &c2) { - return c1.size() < c2.size(); - }); - - std::cout << groups.size() << std::endl; - - - return std::vector>(); + auto compare_func = [](const std::vector &v1, const std::vector &v2) { + return v1.size() < v2.size(); // sort by vector size + }; + std::stable_sort(groups.begin(), groups.end(), compare_func); // using stable sort for ordered index + return groups; } } // namespace klotski