-
Notifications
You must be signed in to change notification settings - Fork 3
/
construct-permutation-via-backtracking.cpp
103 lines (91 loc) · 1.67 KB
/
construct-permutation-via-backtracking.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
* @file construct-permutation-via-backtracking.cpp
* @author prakash ([email protected])
* @brief
* @version 0.1
* @date 2021-07-28
*
* @copyright Copyright (c) 2021
*
*/
#include <iostream>
using namespace std;
#define NMAX 10000
/**
* @brief construct candidates
*
* @param a
* @param k
* @param n
* @param c
* @param nc
*/
void construct_candidates(int a[], int k, int n, int c[], int *nc) {
int i; /* counter */
bool in_perm[NMAX]; /* what is now in perm*/
for (i = 1; i < NMAX; i++) {
in_perm[i] = false;
}
for (i = 1; i < k; i++) {
in_perm[a[i]] = true;
}
*nc = 0;
for (i = 1; i <= n; i++) {
if (!in_perm[i]) { // if not in perm
c[*nc] = i;
*nc = *nc + 1;
}
}
}
/**
* @brief process solution
*
* @param a
* @param k
* @param input
*/
void process_solution(int a[], int k, int input) {
int i;
std::cout << "{ ";
for (i = 1; i <= k; i++) {
std::cout << a[i] << " ";
}
std::cout << "}" << std::endl;
}
/**
* @brief is_a_solution
*
* @param a
* @param k
* @param n
* @return is
*/
int is_a_solution(int a[], int k, int n) { return (k == n); }
void backtrack(int a[], int k, int n) {
int i;
int c[NMAX];
int nc;
if (is_a_solution(a, k, n)) {
process_solution(a, k, n);
} else {
k = k + 1;
construct_candidates(a, k, n, c, &nc);
for (i = 0; i < nc; i++) {
a[k] = c[i];
backtrack(a, k, n);
}
}
}
/**
* @brief generate_permutations
*
* @param n
*/
void generate_permutations(int n) {
int a[NMAX]; /* solution vector */
backtrack(a, 0, n);
}
int main(int argc, const char **argv) {
generate_permutations(3);
return 0;
}