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
| :- use_module(library(clpfd)).
sudoku(Lignes) :-
append(Lignes, Vs), Vs ins 1..16,
maplist(all_distinct, Lignes),
transpose(Lignes, Colonnes),
maplist(all_distinct, Colonnes),
Lignes = [A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P],
blocks(A, B, C, D), blocks(E, F, G, H), blocks(I, J, K, L), blocks(M, N, O, P),
flatten(Lignes, Flatten_Lignes),
labeling([ff],Flatten_Lignes),
maplist(writeln, Lignes).
blocks([], [], [], []).
blocks([A,B,C,D|Bs1], [E,F,G,H|Bs2], [I,J,K,L|Bs3], [M,N,O,P|Bs4]) :-
all_distinct([A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P]),
blocks(Bs1, Bs2, Bs3, Bs4).
:- time(sudoku([[3, 8, 9, 12, 14, 16, 1, 5, 4, 15, 13, 11, 10, 6, 2, 7],
[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _],
[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _],
[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _],
[7, 5, 13, 16, 15, 1, 8, 4, 11, 14, 10, 12, 9, 3, 6, 2],
[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _],
[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _],
[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _],
[2, 14, 10, 15, 8, 12, 13, 11, 7, 9, 3, 5, 16, 4, 1, 6],
[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _],
[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _],
[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _],
[13, 12, 1, 6, 10, 11, 5, 9, 2, 16, 4, 3, 8, 15, 7, 14],
[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _],
[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _],
[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _]])). |
Partager