Skip to content

Commit bcdd7bb

Browse files
committed
Update magic_sequence.cpp
1 parent cdaaaf8 commit bcdd7bb

File tree

1 file changed

+59
-10
lines changed

1 file changed

+59
-10
lines changed

backtracking/magic_sequence.cpp

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,57 @@
11
/*
22
* @brief [Magic sequence](https://www.csplib.org/Problems/prob019/) implementation
33
*
4-
* @details
4+
* @details Solve the magic sequence problem with a backtraking
5+
*
6+
* "A magic sequence of length $n$ is a sequence of integers $x_0
7+
* \ldots x_{n-1}$ between $0$ and $n-1$, such that for all $i$
8+
* in $0$ to $n-1$, the number $i$ occurs exactly $x_i$ times in
9+
* the sequence. For instance, $6,2,1,0,0,0,1,0,0,0$ is a magic
10+
* sequence since $0$ occurs $6$ times in it, $1$ occurs twice, etc."
11+
* Quote of https://www.csplib.org/Problems/prob019/
512
*
613
* @author [Jxtopher](https://github.com/jxtopher)
714
*/
815

9-
#include <algorithm>
10-
#include <cassert>
11-
#include <iostream>
12-
#include <list>
13-
#include <numeric>
14-
#include <vector>
16+
#include <algorithm> /// std::count
17+
#include <cassert> /// assert
18+
#include <iostream> /// IO operations
19+
#include <list> /// std::list
20+
#include <numeric> /// std::accumulate
21+
#include <vector> /// std::vector
1522

23+
/**
24+
* @namespace
25+
* @brief Backtracking algorithms
26+
*/
1627
namespace backtracking {
28+
29+
/**
30+
* @namespace
31+
* @brief Definition and solve magic sequence problem
32+
*/
1733
namespace magic_sequence {
1834
using sequence_t = std::vector<unsigned int>;
1935

36+
37+
/**
38+
* @brief print a magic sequence
39+
*
40+
* @param s a magic sequence
41+
*/
2042
void print(const sequence_t& s) {
2143
for (const auto& item : s) std::cout << item << " ";
2244
std::cout << std::endl;
2345
}
2446

25-
// Check if it's a magic sequence
47+
/**
48+
* @brief Check if it's a magic sequence
49+
*
50+
* @param s a magic sequence
51+
* @return true if is a magic sequence
52+
* @return false otherwise
53+
*
54+
*/
2655
bool is_magic(const sequence_t& s) {
2756
for (unsigned int i = 0; i < s.size(); i++) {
2857
if (std::count(s.cbegin(), s.cend(), i) != s[i]) {
@@ -32,13 +61,28 @@ bool is_magic(const sequence_t& s) {
3261
return true;
3362
}
3463

35-
// Filtering of sub-solutions
36-
// true if the sub-solution is valid otherwise false
64+
/**
65+
* @brief Filtering of sub-solutions
66+
*
67+
* @param s a magic sequence
68+
* @param depth
69+
* @return true if the sub-solution is valid
70+
* @return false otherwise
71+
*
72+
*/
3773
bool filtering(const sequence_t& s, unsigned int depth) {
3874
return std::accumulate(s.cbegin(), s.cbegin() + depth,
3975
static_cast<unsigned int>(0)) <= s.size();
4076
}
4177

78+
/**
79+
* @brief solve magic squance problem
80+
*
81+
* @param s a magic sequence
82+
* @param ret list of valid magic sequences
83+
* @param depth depth in the tree
84+
*
85+
*/
4286
void solve(sequence_t* s, std::list<sequence_t>* ret, unsigned int depth = 0) {
4387
if (depth == s->size()) {
4488
if (is_magic(*s)) {
@@ -58,6 +102,11 @@ void solve(sequence_t* s, std::list<sequence_t>* ret, unsigned int depth = 0) {
58102

59103
} // namespace backtracking
60104

105+
106+
/**
107+
* @brief tests
108+
*
109+
*/
61110
static void test() {
62111
backtracking::magic_sequence::sequence_t s_magic = {6, 2, 1, 0, 0,
63112
0, 1, 0, 0, 0};

0 commit comments

Comments
 (0)