| 1 |
/*** |
| 2 |
!Test |
| 3 |
expected: |
| 4 |
- !Result |
| 5 |
solution: !Solution |
| 6 |
puzzle: |
| 7 |
- [5, 9, 3, 7, 6, 2, 8, 1, 4] |
| 8 |
- [2, 6, 8, 4, 3, 1, 5, 7, 9] |
| 9 |
- [7, 1, 4, 9, 8, 5, 2, 3, 6] |
| 10 |
- [3, 2, 6, 8, 5, 9, 1, 4, 7] |
| 11 |
- [1, 8, 7, 3, 2, 4, 9, 6, 5] |
| 12 |
- [4, 5, 9, 1, 7, 6, 3, 2, 8] |
| 13 |
- [9, 4, 2, 6, 1, 8, 7, 5, 3] |
| 14 |
- [8, 3, 5, 2, 4, 7, 6, 9, 1] |
| 15 |
- [6, 7, 1, 5, 9, 3, 4, 8, 2] |
| 16 |
***/ |
| 17 |
|
| 18 |
% |
| 19 |
%-----------------------------------------------------------------------------% |
| 20 |
% Sudoku for squares of arbitrary size N = (S x S) |
| 21 |
%-----------------------------------------------------------------------------% |
| 22 |
|
| 23 |
int: S; |
| 24 |
int: N = S * S; |
| 25 |
|
| 26 |
|
| 27 |
array[1..N,1..N] of var 1..N: puzzle; |
| 28 |
|
| 29 |
|
| 30 |
include "alldifferent.mzn"; |
| 31 |
|
| 32 |
% All cells in a row, in a column, and in a subsquare are different. |
| 33 |
constraint |
| 34 |
forall(i in 1..N)( alldifferent(j in 1..N)( puzzle[i,j] )) |
| 35 |
/\ |
| 36 |
forall(j in 1..N)( alldifferent(i in 1..N)( puzzle[i,j] )) |
| 37 |
/\ |
| 38 |
forall(i,j in 1..S) |
| 39 |
( alldifferent(p,q in 1..S)( puzzle[S*(i-1)+p, S*(j-1)+q] )); |
| 40 |
|
| 41 |
|
| 42 |
solve satisfy; |
| 43 |
|
| 44 |
|
| 45 |
output [ "sudoku:\n" ] ++ |
| 46 |
[ show(puzzle[i,j]) ++ |
| 47 |
if j = N then |
| 48 |
if i mod S = 0 /\ i < N then "\n\n" else "\n" endif |
| 49 |
else |
| 50 |
if j mod S = 0 then " " else " " endif |
| 51 |
endif |
| 52 |
| i,j in 1..N ]; |
| 53 |
|
| 54 |
%-----------------------------------------------------------------------------% |
| 55 |
% |
| 56 |
% The data for the puzzle that causes satz to make 1 backtrack (normally none |
| 57 |
% are made). |
| 58 |
% |
| 59 |
|
| 60 |
S=3; |
| 61 |
puzzle=[| |
| 62 |
_, _, _, _, _, _, _, _, _| |
| 63 |
_, 6, 8, 4, _, 1, _, 7, _| |
| 64 |
_, _, _, _, 8, 5, _, 3, _| |
| 65 |
_, 2, 6, 8, _, 9, _, 4, _| |
| 66 |
_, _, 7, _, _, _, 9, _, _| |
| 67 |
_, 5, _, 1, _, 6, 3, 2, _| |
| 68 |
_, 4, _, 6, 1, _, _, _, _| |
| 69 |
_, 3, _, 2, _, 7, 6, 9, _| |
| 70 |
_, _, _, _, _, _, _, _, _| |
| 71 |
|]; |
| 72 |
|