-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbacktracking-template.cpp
87 lines (71 loc) · 1.58 KB
/
backtracking-template.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
/**
* @file backtracking.cpp
* @author prakash ([email protected])
* @brief Backtracking constructs a tree of partial solutions,
where each node represents a partial solution
* @version 0.1
* @date 2021-07-27
*
* @copyright Copyright (c) 2021
*
*/
#include <iostream>
using namespace std;
#define MAXCANDIDATES 100
bool finished = false;
/**
* @brief Custom Data
*
*/
typedef struct Data {
int value;
int index;
Data(int v, int i) : value(v), index(i) {}
} Data;
// is_a_solution
bool is_a_solution(int a[], int k, Data input) {
return true;
}
// process solution
void process_solution(int a[], int k, Data input) {
}
//construct_candiadtes
void construct_candidates(int a[], int k, Data input, int c[], int n) {
}
//make_move
void make_move(int a[], int k, Data input){
}
void unmake_move(int a[], int k, Data input){
}
/**
* @brief backtracking function
*
* @param a
* @param k
* @param input
*/
void backtrack(int a[], int k, Data input) {
int c[MAXCANDIDATES]; /*candidate for next position*/
int nc; /* next position candidate count */
int i; /* counter */
if (is_a_solution(a, k, input)) {
finished = true;
process_solution(a, k, input);
} else {
k = k + 1;
construct_candidates(a, k, input, c, nc);
for (i = 0; i < nc; i++) {
a[k] = c[i];
make_move(a, k, input);
backtrack(a, k, input);
unmake_move(a, k, input);
if (finished) {
return; /* terminate early */
}
}
}
}
int main(int argc, const char **argv) {
/*code here*/
return 0;
}