37 std::vector<unsigned int>;
42void print(
const sequence_t& s) {
43 for (
const auto& item : s) std::cout << item <<
" ";
44 std::cout << std::endl;
53bool is_magic(
const sequence_t& s) {
54 for (
unsigned int i = 0; i < s.size(); i++) {
55 if (std::count(s.cbegin(), s.cend(), i) != s[i]) {
69bool filtering(
const sequence_t& s,
unsigned int depth) {
70 return std::accumulate(s.cbegin(), s.cbegin() + depth,
71 static_cast<unsigned int>(0)) <= s.size();
80void solve(sequence_t* s, std::list<sequence_t>* ret,
unsigned int depth = 0) {
81 if (depth == s->size()) {
86 for (
unsigned int i = 0; i < s->size(); i++) {
88 if (filtering(*s, depth + 1)) {
89 solve(s, ret, depth + 1);
104 backtracking::magic_sequence::sequence_t s_magic = {6, 2, 1, 0, 0,
106 assert(backtracking::magic_sequence::is_magic(s_magic));
109 backtracking::magic_sequence::sequence_t s_not_magic = {5, 2, 1, 0, 0,
111 assert(!backtracking::magic_sequence::is_magic(s_not_magic));
122 for (
unsigned int i = 2; i < 12; i++) {
123 std::cout <<
"Solution for n = " << i << std::endl;
125 std::list<backtracking::magic_sequence::sequence_t> list_of_solutions;
127 backtracking::magic_sequence::sequence_t s1(i, i);
129 backtracking::magic_sequence::solve(&s1, &list_of_solutions);
131 for (
const auto& item : list_of_solutions) {
132 backtracking::magic_sequence::print(item);
bool solve(int x, int y, int mov, std::array< std::array< int, V >, V > &sol, const std::array< int, V > &xmov, std::array< int, V > &ymov)
Functions for the Magic sequence implementation.