Recent Changes - Search:

PmWiki

pmwiki.org

edit SideBar

Sudoku

sudoku(P) :-

  fd_domain(P,1,9),
  Xs = [1,2,3,4,5,6,7,8,9],
  row(P,Xs),
  col(P,Xs),
  unit(P,Xs),
  fd_labeling(P).

row(_,[]). row(P,[X|Xs]) :-

  setof(V,[C,I]^(for(C,1,9),I is (X-1)*9+C,nth(I,P,V)),L1),
  fd_all_different(L1),
  row(P,Xs).

col(_,[]). col(P,[X|Xs]) :-

  setof(V,[R,I]^(for(R,1,9),I is (R-1)*9+X,nth(I,P,V)),L2),
  fd_all_different(L2),
  col(P,Xs).unit(_,[]).

unit(P,[U|Us]) :-

  Cs is ((U-1) mod 3)*3+1, Ce is Cs+2,
  Rs is ((U-1) // 3)*3+1, Re is Rs+2,
  setof(V,[R,C,I]^(for(R,Rs,Re),for(C,Cs,Ce),I is (R-1)*9+C,nth(I,P,V)),L),
  fd_all_different(L),
  unit(P,Us).
Edit - History - Print - Recent Changes - Search
Page last modified on August 25, 2008, at 05:29 PM