all_diff.c
1.35 KB
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
/*
* all_diff.c
*
* Created on: 15/10/2015
* Author: pedro
*
* All_diff constraint:
* Solve a CSP with 1 all_different constraint with N variables with N values each.
*/
#include "all_diff.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include "../config.h"
#include "../constraints/all_different.h"
#include "../split.h"
#include "../variables.h"
/*
* Solve one single all_diff constraint with N variables and N values each
*/
void run_all_diff(int* csp_dims) {
int n = csp_dims[0];
unsigned long result;
int i;
// vector with the IDs of the variables constrained by the all_diff constraint
unsigned int* c_vs = malloc((unsigned long)n * sizeof(unsigned int));
// Create the CSP variables
for (i = 0; i < n; i++) {
c_vs[i] = v_new_range(0, (unsigned int)n - 1, true);
}
// Create the constraint
c_all_different(c_vs, (unsigned int)n);
if (FINDING_ONE_SOLUTION) {
printf("\nFinding one solution for the all-different constraint with %u variables.\n", n);
} else {
printf("\nCounting all the solutions for the all-different constraint with %u variables.\n", n);
}
// Solve the CSP
result = solve_CSP();
if (FINDING_ONE_SOLUTION && result == 1) {
printf("Solution:\n");
vs_print_single_val(c_vs, (unsigned int)n, 1);
printf("\n");
} else {
printf("%lu solution(s) found\n", result);
}
free(c_vs);
}