Browse Source

update: build all groups

master
Dnomd343 2 years ago
parent
commit
aa4dc085e9
  1. 7
      src/klotski_core/ffi/tmain.cc
  2. 93
      src/klotski_core/group/group.cc

7
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");
}

93
src/klotski_core/group/group.cc

@ -80,98 +80,37 @@ std::vector<RawCode> Group::group_cases(const RawCode &seed) {
std::vector<std::vector<CommonCode>> 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<std::vector<CommonCode>> 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<CommonCode> rs(k.begin(), k.end());
// std::vector<CommonCode> 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<CommonCode> 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<CommonCode> 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<CommonCode> &c1, const std::vector<CommonCode> &c2) {
return c1.size() < c2.size();
});
std::cout << groups.size() << std::endl;
return std::vector<std::vector<CommonCode>>();
auto compare_func = [](const std::vector<CommonCode> &v1, const std::vector<CommonCode> &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

Loading…
Cancel
Save