update code formatter

This commit is contained in:
Jxtopher
2021-07-11 22:52:18 +02:00
parent bc7437044e
commit 60ccf91510
2 changed files with 94 additions and 32 deletions

View File

@@ -15,41 +15,45 @@
namespace backtracking {
namespace magic_sequence {
using sequence_t = std::vector<unsigned int>;
using sequence_t = std::vector<unsigned int>;
void print(const sequence_t& s) {
for(auto item : s) std::cout << item << " ";
std::cout << std::endl;
void print(const sequence_t& s) {
for (const auto& item : s) std::cout << item << " ";
std::cout << std::endl;
}
// Check if it's a magic sequence
bool is_magic(const sequence_t& s) {
for (unsigned int i = 0; i < s.size(); i++) {
if (std::count(s.cbegin(), s.cend(), i) != s[i])
return false;
}
return true;
}
// Check if it's a magic sequence
bool is_magic(const sequence_t& s) {
for(unsigned int i = 0; i < s.size(); i++)
if(std::count(s.cbegin(), s.cend(), i) != s[i]) return false;
return true;
}
// Filtering of sub-solutions
// true if the sub-solution is valid otherwise false
bool filtering(const sequence_t& s, unsigned int depth) {
return std::accumulate(s.cbegin(), s.cbegin() + depth,
static_cast<unsigned int>(0)) <= s.size();
}
// Filtering of sub-solutions
// true if the sub-solution is valid otherwise false
bool filtering(const sequence_t& s, unsigned int depth) {
return std::accumulate(s.cbegin(), s.cbegin() + depth, static_cast<unsigned int>(0)) <=
s.size();
}
void solve(sequence_t& s, std::list<sequence_t>& ret, unsigned int depth = 0) {
if(depth == s.size()) {
if(is_magic(s)) ret.push_back(s);
} else {
for(unsigned int i = 0; i < s.size(); i++) {
s[depth] = i;
if(filtering(s, depth + 1)) solve(s, ret, depth + 1);
}
void solve(sequence_t* s, std::list<sequence_t>* ret, unsigned int depth = 0) {
if (depth == s->size()) {
if (is_magic(*s))
ret->push_back(*s);
} else {
for (unsigned int i = 0; i < s->size(); i++) {
(*s)[depth] = i;
if (filtering(*s, depth + 1))
solve(s, ret, depth + 1);
}
}
}
} // namespace magic_sequence
} // namespace magic_sequence
} // namespace backtracking
} // namespace backtracking
using namespace backtracking::magic_sequence;
@@ -64,11 +68,11 @@ static void test() {
int main() {
test();
for(unsigned int i = 2; i < 12; i++) {
for (unsigned int i = 2; i < 12; i++) {
std::cout << "Solution for n = " << i << std::endl;
std::list<sequence_t> r1;
sequence_t s1(i, i);
std::list<sequence_t>* r1 = new std::list<sequence_t>();
sequence_t* s1 = new sequence_t(i, i);
solve(s1, r1);
for(auto item : r1) print(item);
for (const auto& item : *r1) print(item);
}
}