Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 0.88 KB | None | 0 0
  1. :- use_module(library(clpfd)).
  2. % shorthand for printing the solution of problem X
  3. sud(O) :-
  4.   open(O, write, OF),
  5.   problem(1, Rows), sudoku(Rows), maplist(write(OF), Rows),
  6.   close(OF).
  7.  
  8. % sudoku solving:
  9. sudoku(Rows) :-
  10.   % Rows is a list of the list Vs
  11.   % where Vs contains values from 1 to 9
  12.   append(Rows, Vs), Vs ins 1..9,
  13.   % all values in the lists in Rows have to be distinct
  14.   maplist(all_distinct, Rows),
  15.   % split up Rows into Columns and check them
  16.   transpose(Rows, Columns), maplist(all_distinct, Columns),
  17.   % check the blocks
  18.   Rows = [A,B,C,D,E,F,G,H,I],
  19.   blocks(A, B, C), blocks(D, E, F), blocks(G, H, I),
  20.   % force a definite value for every variable in Rows
  21.   maplist(label, Rows).
  22.  
  23. blocks([], [], []).
  24. blocks([A,B,C|Bs1], [D,E,F|Bs2], [G,H,I|Bs3]) :-
  25.   % all values in a block are distinct
  26.   all_distinct([A,B,C,D,E,F,G,H,I]),
  27.   blocks(Bs1, Bs2, Bs3).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement