magic-square.c
3.53 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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include "fdc_int.h"
// (CSPLib 019)
#define NMAX 32
static int N = 5; // actual order of the square
int main(int argc, char *argv[])
{
fd_int square[NMAX][NMAX], all[NMAX * NMAX];
fd_int numbers[NMAX];
int sum;
int gecode_symmetries = 0, old_symmetries = 0;
int solutions = 0, one_solution = 1;
int l, c, i, j;
fd_init(&argc, &argv);
for (i = 1; i < argc; ++i)
if (!strcmp(argv[i], "--all"))
one_solution = 0;
else if (!strcmp(argv[i], "--gecode"))
gecode_symmetries = 1;
else if (!strcmp(argv[i], "--old"))
old_symmetries = 1;
else
{
N = atoi(argv[i]);
break;
}
if (N > NMAX)
_fd_fatal("magic-square: maximum order exceeded");
j = 0;
for (++i; i < argc; ++i)
{
int k0, k1;
char *p;
k0 = k1 = atoi(argv[i]) - 1;
if (p = strchr(argv[i], '-'))
k1 = atoi(p + 1) - 1;
all[j++] = fd_new(k0, k1);
}
for (; j < N * N; ++j)
all[j] = fd_new(0, N * N - 1);
for (l = 0; l < N; ++l)
for (c = 0; c < N; ++c)
square[l][c] = all[l * N + c];
#if 01
fd_all_different(all, N * N);
#else
for (i = 0; i < N * N; ++i)
fd_exactly(all, N * N, 1, i); // fd_exactly_one(all, N * N, i);
#endif
sum = (N * N - 1) * N * N / 2 / N;
#if 01
// constrain lines
for (l = 0; l < N; l++)
fd_sum(square[l], N, sum);
// constrain columns
for (c = 0; c < N; c++)
{
for (l = 0; l < N; l++)
numbers[l] = square[l][c];
fd_sum(numbers, N, sum);
}
// constrain diagonals
for (l = 0; l < N; l++)
numbers[l] = square[l][l];
fd_sum(numbers, N, sum);
for (l = 0; l < N; l++)
numbers[l] = square[l][N - 1 - l];
fd_sum(numbers, N, sum);
#else
fd_int sv = fd_new(sum, sum);
// constrain lines
for (l = 0; l < N; l++)
fd_sum2(square[l], N, sv);
// constrain columns
for (c = 0; c < N; c++)
{
for (l = 0; l < N; l++)
numbers[l] = square[l][c];
fd_sum2(numbers, N, sv);
}
// constrain diagonals
for (l = 0; l < N; l++)
numbers[l] = square[l][l];
fd_sum2(numbers, N, sv);
for (l = 0; l < N; l++)
numbers[l] = square[l][N - 1 - l];
fd_sum2(numbers, N, sv);
#endif
if (one_solution)
{
// break some symmetries
if (gecode_symmetries) // from Gecode
{
fd_gt(square[0][0], square[0][N - 1]);
fd_gt(square[0][0], square[N - 1][0]);
}
else if (old_symmetries)
{
// disallow rotations and some reflections
fd_lt(square[0][0], square[0][N - 1]);
fd_lt(square[N - 1][0], square[0][N - 1]);
fd_lt(square[0][0], square[N - 1][N - 1]);
}
else
{
// disallow reflections and rotations
#if 0
fd_lt(square[0][0], square[0][N - 1]);
fd_lt(square[0][N - 1], square[N - 1][0]);
#else
fd_lt(square[0][0], square[N - 1][0]);
fd_lt(square[N - 1][0], square[0][N - 1]);
#endif
fd_lt(square[0][0], square[N - 1][N - 1]);
}
}
while (fd_solve() == FD_OK)
{
printf("solution %d:\n", ++solutions);
#if 0
for (l = 0; l < N; ++l)
for (c = 0; c < N; ++c)
if (!fd_var_single(square[l][c], NULL))
_fd_fatal("solution contains non-singleton variable");
#endif
for (l = 0; l < N; ++l)
{
for (c = 0; c < N; ++c)
printf(" %2d", fd_var_value(square[l][c]) + 1);
putchar('\n');
}
#if !(defined(LOCAL_SEARCH) || defined(DISTRIBUTED_SOLVER))
if (one_solution)
#endif
break;
}
if (solutions)
printf("%d solutions found\n", solutions);
else
printf("inconsistent CSP\n");
fd_end();
return !solutions;
}