Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
26
Task/Sudoku/Prolog/sudoku-1.pro
Normal file
26
Task/Sudoku/Prolog/sudoku-1.pro
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
:- use_module(library(clpfd)).
|
||||
|
||||
sudoku(Rows) :-
|
||||
length(Rows, 9), maplist(length_(9), Rows),
|
||||
append(Rows, Vs), Vs ins 1..9,
|
||||
maplist(all_distinct, Rows),
|
||||
transpose(Rows, Columns), maplist(all_distinct, Columns),
|
||||
Rows = [A,B,C,D,E,F,G,H,I],
|
||||
blocks(A, B, C), blocks(D, E, F), blocks(G, H, I).
|
||||
|
||||
length_(L, Ls) :- length(Ls, L).
|
||||
|
||||
blocks([], [], []).
|
||||
blocks([A,B,C|Bs1], [D,E,F|Bs2], [G,H,I|Bs3]) :-
|
||||
all_distinct([A,B,C,D,E,F,G,H,I]),
|
||||
blocks(Bs1, Bs2, Bs3).
|
||||
|
||||
problem(1, [[_,_,_,_,_,_,_,_,_],
|
||||
[_,_,_,_,_,3,_,8,5],
|
||||
[_,_,1,_,2,_,_,_,_],
|
||||
[_,_,_,5,_,7,_,_,_],
|
||||
[_,_,4,_,_,_,1,_,_],
|
||||
[_,9,_,_,_,_,_,_,_],
|
||||
[5,_,_,_,_,_,_,7,3],
|
||||
[_,_,2,_,1,_,_,_,_],
|
||||
[_,_,_,_,4,_,_,_,9]]).
|
||||
54
Task/Sudoku/Prolog/sudoku-2.pro
Normal file
54
Task/Sudoku/Prolog/sudoku-2.pro
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
:- initialization(main).
|
||||
|
||||
|
||||
solve(Rows) :-
|
||||
maplist(domain_1_9, Rows)
|
||||
, different(Rows)
|
||||
, transpose(Rows,Cols), different(Cols)
|
||||
, blocks(Rows,Blocks) , different(Blocks)
|
||||
, maplist(fd_labeling, Rows)
|
||||
.
|
||||
|
||||
domain_1_9(Rows) :- fd_domain(Rows,1,9).
|
||||
different(Rows) :- maplist(fd_all_different, Rows).
|
||||
|
||||
blocks(Rows,Blocks) :-
|
||||
maplist(split3,Rows,Xs), transpose(Xs,Ys)
|
||||
, concat(Ys,Zs), concat_map(split3,Zs,Blocks)
|
||||
. % where
|
||||
split3([X,Y,Z|L],[[X,Y,Z]|R]) :- split3(L,R).
|
||||
split3([],[]).
|
||||
|
||||
|
||||
% utils/list
|
||||
concat_map(F,Xs,Ys) :- call(F,Xs,Zs), maplist(concat,Zs,Ys).
|
||||
|
||||
concat([],[]).
|
||||
concat([X|Xs],Ys) :- append(X,Zs,Ys), concat(Xs,Zs).
|
||||
|
||||
transpose([],[]).
|
||||
transpose([[X]|Col], [[X|Row]]) :- transpose(Col,[Row]).
|
||||
transpose([[X|Row]], [[X]|Col]) :- transpose([Row],Col).
|
||||
transpose([[X|Row]|Xs], [[X|Col]|Ys]) :-
|
||||
maplist(bind_head, Row, Ys, YX)
|
||||
, maplist(bind_head, Col, Xs, XY)
|
||||
, transpose(XY,YX)
|
||||
. % where
|
||||
bind_head(H,[H|T],T).
|
||||
bind_head([],[],[]).
|
||||
|
||||
|
||||
% tests
|
||||
test([ [_,_,3,_,_,_,_,_,_]
|
||||
, [4,_,_,_,8,_,_,3,6]
|
||||
, [_,_,8,_,_,_,1,_,_]
|
||||
, [_,4,_,_,6,_,_,7,3]
|
||||
, [_,_,_,9,_,_,_,_,_]
|
||||
, [_,_,_,_,_,2,_,_,5]
|
||||
, [_,_,4,_,7,_,_,6,8]
|
||||
, [6,_,_,_,_,_,_,_,_]
|
||||
, [7,_,_,6,_,_,5,_,_]
|
||||
]).
|
||||
|
||||
main :- test(T), solve(T), maplist(show,T), halt.
|
||||
show(X) :- write(X), nl.
|
||||
Loading…
Add table
Add a link
Reference in a new issue