For Programmers: Free Programming Magazines  


Home > Archive > Prolog > November 2005 > Re: Sudoku with SWI Prolog









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: Sudoku with SWI Prolog
Markus Triska

2005-11-10, 7:57 am

Hi!

Mats wrote:

> * SICSTUS CLPFD DEMONSTRATION PROGRAM
> * Purpose : Car Sequencing Problem


Here is a version slightly modified to work with the CVS version of SWI
Prolog:

:- use_module(library(bounds)).
:- use_module(library(clp_distinct)).

suudoku(P) :-
Rows = [R1,R2,R3,R4,R5,R6,R7,R8,R9],
problem(P, Rows),
append_all(Rows, Vars),
vars_in(Vars, 1, 9),
Vars in 1..9,
row_constraint(Rows),
column_constraint(R1, R2, R3, R4, R5, R6, R7, R8, R9),
block_constraint(R1, R2, R3),
block_constraint(R4, R5, R6),
block_constraint(R7, R8, R9),
(labeling([ff], Vars) -> true),
display_rows(Rows).

display_rows([]).
display_rows([[X1,X2,X3,X4,X5,X6,X7,X8,X
9]|Rows]) :-
format('~d ~d ~d ~d ~d ~d ~d ~d ~d \n',
[X1,X2,X3,X4,X5,X6,X7,X8,X9]),
display_rows(Rows).

row_constraint([]).
row_constraint([R|Rt]) :-
all_distinct(R),
row_constraint(Rt).

column_constraint([], [], [], [], [], [], [], [], []).
column_constraint([X1|R1], [X2|R2], [X3|R3], [X4|R4], [X5|R5], [X6|R6],
[X7|R7], [X8|R8], [X9|R9]) :-
all_distinct([X1,X2,X3,X4,X5,X6,X7,X8,X9
]),
column_constraint(R1, R2, R3, R4, R5, R6, R7, R8, R9).


block_constraint([], [], []).
block_constraint([X1,X2,X3|R1], [X4,X5,X6|R2], [X7,X8,X9|R3]) :-
all_distinct([X1,X2,X3,X4,X5,X6,X7,X8,X9
]),
block_constraint(R1, R2, R3).

append_all([], []).
append_all([P|R], X) :-
append(P, Y, X),
append_all(R, Y).

problem(1, P) :- % shokyuu
P=[[1,_,_,8,_,4,_,_,_],
[_,2,_,_,_,_,4,5,6],
[_,_,3,2,_,5,_,_,_],
[_,_,_,4,_,_,8,_,5],
[7,8,9,_,5,_,_,_,_],
[_,_,_,_,_,6,2,_,3],
[8,_,1,_,_,_,7,_,_],
[_,_,_,1,2,3,_,8,_],
[2,_,5,_,_,_,_,_,9]].

problem(2, P) :- % shokyuu
P=[[_,_,2,_,3,_,1,_,_],
[_,4,_,_,_,_,_,3,_],
[1,_,5,_,_,_,_,8,2],
[_,_,_,2,_,_,6,5,_],
[9,_,_,_,8,7,_,_,3],
[_,_,_,_,4,_,_,_,_],
[8,_,_,_,7,_,_,_,4],
[_,9,3,1,_,_,_,6,_],
[_,_,7,_,6,_,5,_,_]].

problem(3, P) :- % chuukyuu
P=[[_,_,_,_,_,_,3,_,_],
[_,_,_,8,5,_,_,1,_],
[_,_,2,_,_,4,_,_,9],
[_,3,_,_,_,2,_,_,4],
[8,_,_,_,6,_,_,_,1],
[7,_,_,9,_,_,_,5,_],
[1,_,_,6,_,_,7,_,_],
[_,9,_,_,2,3,_,_,_],
[_,_,4,_,_,_,_,_,_]].

problem(4, P) :- % joukyuu
P=[[_,7,9,_,_,_,_,_,1],
[6,_,_,_,_,_,3,8,_],
[_,_,_,_,4,2,_,_,_],
[_,_,3,9,_,_,_,_,_],
[7,8,_,_,_,_,_,2,5],
[_,_,_,_,_,4,8,_,_],
[_,_,_,3,1,_,_,_,_],
[_,5,6,_,_,_,_,_,7],
[2,_,_,_,_,_,4,3,_]].

problem(5, P) :- % shokyuu; from Mr. Horai
P=[[_,5,_,7,_,1,_,4,_],
[7,_,3,_,_,_,1,_,2],
[_,8,_,4,_,6,_,9,_],
[9,_,4,_,6,_,8,_,3],
[_,_,_,8,_,7,_,_,_],
[1,_,8,_,5,_,6,_,9],
[_,1,_,6,_,3,_,8,_],
[5,_,6,_,_,_,7,_,1],
[_,3,_,5,_,9,_,2,_]].

problem(6, P) :- % Hard: suudoku2 99 (1989)
P=[[8,_,_,_,_,5,_,_,_],
[_,1,2,3,_,_,6,_,_],
[_,4,5,6,_,_,_,2,_],
[_,7,8,_,_,_,_,_,1],
[_,_,_,_,9,_,_,_,_],
[9,_,_,_,_,_,8,7,_],
[_,2,_,_,_,6,5,4,_],
[_,_,4,_,_,3,2,1,_],
[_,_,_,1,_,_,_,_,9]].


Best regards,
Markus.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com