52 lines
1.4 KiB
Prolog
52 lines
1.4 KiB
Prolog
|
|
:- use_module(library(clpfd)).
|
|
|
|
sudoku(Rows) :-
|
|
length(Rows, 9),
|
|
maplist(same_length(Rows), Rows),
|
|
append(Rows, Vs),
|
|
Vs ins 1..9,
|
|
maplist(all_distinct, Rows),
|
|
transpose(Rows, Columns),
|
|
maplist(all_distinct, Columns),
|
|
Rows = [As,Bs,Cs,Ds,Es,Fs,Gs,Hs,Is],
|
|
square(As, Bs, Cs),
|
|
square(Ds, Es, Fs),
|
|
square(Gs, Hs, Is).
|
|
|
|
square([], [], []).
|
|
square([N1,N2,N3|Ns1],
|
|
[N4,N5,N6|Ns2],
|
|
[N7,N8,N9|Ns3]) :-
|
|
all_distinct([N1,N2,N3,N4,N5,N6,N7,N8,N9]),
|
|
square(Ns1, Ns2, Ns3).
|
|
|
|
problem(1, [[_,_,_,_,_,4,_,_,2],
|
|
[_,6,_,2,_,_,_,3,_],
|
|
[_,8,_,_,_,3,5,_,9],
|
|
[_,4,_,_,_,_,1,_,_],
|
|
[1,_,_,7,_,5,_,_,_],
|
|
[5,_,3,_,_,_,_,_,_],
|
|
[_,9,_,3,_,_,_,_,_],
|
|
[_,_,4,_,6,1,_,_,_],
|
|
[_,_,5,_,_,_,7,_,_]]).
|
|
|
|
problem(2, [[_,_,9,5,_,_,_,3,7],
|
|
[1,3,7,9,_,_,_,5,2],
|
|
[2,_,_,_,_,3,6,9,_],
|
|
[3,5,2,_,1,_,_,_,6],
|
|
[_,_,_,4,5,2,3,_,_],
|
|
[_,8,1,_,3,_,2,_,_],
|
|
[6,_,3,_,4,_,8,_,9],
|
|
[5,2,_,_,_,1,_,6,_],
|
|
[_,_,_,3,_,7,_,_,_]]).
|
|
|
|
problem(3, [[_,5,_,1,_,_,_,_,_],
|
|
[2,_,_,5,_,_,6,_,_],
|
|
[1,_,_,_,8,_,2,_,_],
|
|
[_,8,_,4,3,_,_,_,_],
|
|
[_,_,_,_,_,_,_,4,_],
|
|
[_,_,_,_,_,7,9,3,2],
|
|
[_,4,_,6,7,_,_,_,_],
|
|
[_,7,_,_,_,_,_,1,9],
|
|
[9,_,_,_,_,8,_,_,_]]). |