Prolog Sudoku Solver

buy diazepam without prescription
online alprazolam
buy synthroid
buy flagyl online
buy lorazepam without prescription
prozac online cheap
lorazepam pharmacy
xanax pills
plavix online
online valium
order diazepam
generic synthroid
alprazolam pills
cialis prescription
cheap alprazolam tablets
valium prescription
accutane pills
pharmacy cialis
diazepam cheap
online clomid
prozac sale
where to buy xanax
cheapest acomplia
pharmacy clomid
plavix
cheapest soma prices
purchase synthroid
bactrim no prescription
buy clomid without prescription
diazepam no prescription
soma pills
where to buy propecia
order phentermine online
order plavix online
buy cheap zoloft
prozac
viagra
lasix online cheap
order tramadol online
clomid generic
cheap acomplia online
buy cheap zoloft online
cheapest tramadol
purchase prozac online
lorazepam for sale
order plavix
cheap lorazepam
alprazolam pharmacy
valium without a prescription
buy tramadol cheap
prozac without prescription
discount prozac
order xanax
discount nexium
tramadol discount
cheap generic cialis
cheap tramadol online
order zoloft
cheap valium
viagra no prescription
propecia cheap
propecia no prescription
generic phentermine
buy generic cialis
buy generic nexium
order prozac
buy valium online
cheap clomid
doxycycline no prescription
prozac for sale
alprazolam no prescription
propecia
cheap generic lorazepam
price of viagra
lorazepam generic
valium prices
cheap prozac
buy plavix
cheap accutane tablets
pharmacy plavix
tramadol sale
buy plavix online
lasix pills
clomid without a prescription
buy cheap tramadol
order diazepam online
doxycycline prescription
buy plavix without prescription
purchase propecia online
acomplia prices
doxycycline without prescription
price of clomid
order lorazepam online
levitra no prescription
cheap zoloft online
buy doxycycline online
cheapest cialis prices
buy cheap flagyl online
soma
buy prozac without prescription
accutane pharmacy
zoloft online
discount zoloft
discount alprazolam
buy generic soma
buy clomid
price of synthroid
cheap generic acomplia
zoloft
phentermine without a prescription
cheap nexium online
cheapest valium
cheapest nexium
tramadol generic
synthroid prices
doxycycline cheap
buy diazepam online
order bactrim online
buy synthroid online
buy cheap nexium
buy zoloft without prescription
order acomplia online
order levitra
buy zithromax cheap
discount cialis
valium discount
order clomid
cheap accutane
zoloft without a prescription
lowest price cialis
synthroid discount
lowest price zithromax
cheap xanax
buy accutane online
synthroid for sale
soma pharmacy
flagyl for sale
order viagra
cialis cheap
buy generic viagra
discount zithromax
tramadol pills
discount phentermine
tramadol pharmacy
where to buy accutane
buy levitra without prescription
lowest price valium
generic clomid
soma cheap
flagyl without prescription
generic zithromax
accutane without a prescription
cialis for sale
flagyl prescription
doxycycline
levitra for sale
prozac cheap
cheap lasix online
order synthroid
purchase acomplia online
pharmacy nexium
cheap generic levitra
order doxycycline
order soma
zithromax online cheap
zithromax online stores
valium generic
clomid
cheap zithromax
buy alprazolam
buy valium without prescription
buy bactrim without prescription
lowest price nexium
cheap propecia tablets
online acomplia
cheap cialis online
buy cheap accutane
buy tramadol
phentermine pharmacy
alprazolam discount
valium cheap
cheap lasix
accutane
synthroid cheap
zithromax online
alprazolam online
buy clomid cheap
purchase zoloft online
cheap doxycycline online
cheapest bactrim prices
cheap flagyl
acomplia online cheap
zithromax without a prescription
propecia pharmacy
bactrim pills
pharmacy flagyl
purchase nexium
lowest price accutane
cheap nexium
buy levitra
cheap synthroid
pharmacy zithromax
discount acomplia
where to buy alprazolam
cheap clomid tablets
zoloft generic
soma for sale
flagyl without a prescription
viagra prices
viagra online cheap
buy generic phentermine
synthroid pharmacy
buy zoloft cheap
order flagyl
bactrim online stores
order alprazolam online
lorazepam online
purchase doxycycline
doxycycline discount
plavix without a prescription
viagra prescription
online viagra
lowest price zoloft
viagra cheap
plavix online cheap
generic flagyl
price of tramadol
cheap lasix tablets
buy propecia cheap
buy cheap accutane online
cheapest diazepam
bactrim
where to buy tramadol
lorazepam
discount tramadol
cheap phentermine
online tramadol
cheap alprazolam online
clomid no prescription
buy cialis cheap
soma generic
cheap acomplia
purchase plavix
cheap accutane online
viagra generic
cheap viagra
nexium discount
purchase flagyl online
synthroid without a prescription
bactrim without a prescription
buy acomplia cheap
cheap bactrim online
alprazolam prices
generic bactrim
flagyl discount
levitra sale
nexium sale
phentermine
propecia sale
plavix cheap
nexium prices
pharmacy synthroid
cheapest lorazepam prices
where to buy flagyl
cheapest xanax
order cialis online
cheap generic propecia
purchase xanax online
pharmacy accutane
buy generic acomplia
buy levitra online
buy generic plavix
order lasix
buy lorazepam
cheap phentermine tablets
order clomid online
buy cheap acomplia online
buy cheap lorazepam
clomid online stores
buy tramadol online
lasix
buy cheap propecia online
acomplia cheap
bactrim online
buy nexium
generic levitra
price of nexium
phentermine online stores
levitra without a prescription
xanax for sale
buy cheap doxycycline online
lasix pharmacy
where to buy lorazepam
cheap nexium tablets
price of acomplia
zoloft online cheap
discount accutane
flagyl pills
online cialis
nexium without prescription
cheap xanax online
pharmacy zoloft
propecia generic
diazepam for sale
xanax
purchase prozac
cheapest zithromax
buy cheap flagyl
acomplia for sale
price of lasix
generic doxycycline
prozac pharmacy
cheap flagyl tablets
pharmacy levitra
alprazolam prescription
cheapest lasix
viagra online stores
buy xanax without prescription
buy accutane cheap
cheapest lorazepam
cheapest bactrim
cialis pills
lasix without a prescription
phentermine discount
buy cheap xanax online
online levitra
order alprazolam
bactrim cheap
buy cheap propecia
tramadol online stores
cheap diazepam tablets
discount clomid
xanax online cheap
buy generic accutane
synthroid sale
price of flagyl
purchase lasix online
levitra cheap
cheapest prozac prices
xanax no prescription
cialis online
purchase lorazepam online
cheapest acomplia prices
buy flagyl
diazepam online
diazepam without prescription
cialis prices
purchase lorazepam
flagyl prices
phentermine for sale
flagyl
buy clomid online
buy cheap cialis
where to buy zithromax
purchase viagra
where to buy nexium
levitra prescription
plavix prices
buy phentermine cheap
zoloft pills
order doxycycline online
pharmacy alprazolam
lorazepam prescription
zoloft pharmacy
cheapest diazepam prices
soma no prescription
acomplia pills
clomid without prescription
purchase valium
alprazolam cheap
cheapest synthroid
doxycycline online stores
purchase acomplia
tramadol prescription
generic soma
soma sale
cheap prozac online
online nexium
purchase diazepam
doxycycline pills
levitra online
nexium online
viagra sale
synthroid online cheap
acomplia without a prescription
lowest price soma
soma online
diazepam discount
cheap generic diazepam
buy generic alprazolam
order phentermine
valium no prescription
zithromax sale
lowest price clomid
phentermine pills
cheap cialis tablets
alprazolam sale
cheapest xanax prices
purchase levitra
tramadol for sale
purchase zithromax
soma prices
order cialis
purchase clomid online
pharmacy lasix
purchase plavix online
purchase alprazolam online
lowest price acomplia
cheapest zoloft
accutane prices
synthroid online stores
cheap bactrim
cheapest levitra prices
price of lorazepam
pharmacy viagra
where to buy synthroid
where to buy clomid
cheapest zoloft prices
cheap zoloft tablets
zithromax for sale
buy generic flagyl
online lorazepam
clomid online cheap
clomid online
cheap zithromax tablets
cheapest flagyl prices
price of zoloft
online accutane
buy cheap diazepam online
alprazolam online stores
buy generic diazepam
purchase propecia
online propecia
buy cheap diazepam
order accutane online
purchase levitra online
levitra prices
cheap generic synthroid
acomplia online
cheap generic plavix
price of plavix
cheap viagra tablets
order accutane
acomplia generic
valium without prescription
price of accutane
zithromax cheap
lasix cheap
price of propecia
discount synthroid
pharmacy lorazepam
buy cheap phentermine
cheap generic bactrim
buy doxycycline
buy alprazolam cheap
cheapest accutane prices
alprazolam without prescription
cheapest viagra prices
price of bactrim
soma discount
generic valium
generic nexium
diazepam prescription
buy cheap prozac
order valium
buy cheap acomplia
valium online stores
soma online cheap
lowest price lasix
propecia without a prescription
zoloft for sale
cheap generic clomid
discount bactrim
tramadol without a prescription
buy cheap bactrim
cheapest phentermine prices
accutane online
purchase valium online
generic alprazolam
cheapest clomid prices
lorazepam without a prescription
where to buy lasix
lowest price xanax
accutane online stores
buy cheap tramadol online
zoloft prices
purchase phentermine
buy cheap alprazolam
price of cialis
lorazepam discount
purchase tramadol online
buy generic propecia
nexium generic
buy diazepam cheap
buy generic clomid
acomplia pharmacy
pharmacy soma
order valium online
levitra generic
buy prozac online
buy cheap lorazepam online
cheapest prozac
online doxycycline
cheap generic zoloft
doxycycline pharmacy
buy propecia online
propecia prescription
viagra pharmacy
buy flagyl cheap
lasix sale
prozac prescription
buy cheap synthroid
buy soma cheap
generic zoloft
acomplia
purchase soma
bactrim pharmacy
zithromax without prescription
xanax prices
lasix prescription
buy propecia
prozac prices
buy zoloft
purchase tramadol
prozac online stores
price of zithromax
lowest price alprazolam
nexium without a prescription
where to buy plavix
cialis sale
diazepam pills
buy nexium online
generic propecia
buy cialis
discount plavix
cheapest levitra
levitra
cheapest valium prices
soma without a prescription
bactrim online cheap
price of diazepam
cheapest propecia prices
plavix without prescription
buy prozac
propecia pills
nexium for sale
purchase alprazolam
lasix online
buy alprazolam online
where to buy phentermine
plavix online stores
online xanax
prozac no prescription
prozac discount
buy generic xanax
lowest price flagyl
online bactrim
diazepam sale
discount xanax
levitra without prescription
cheap flagyl online
lasix online stores
zithromax
acomplia sale
buy viagra online
buy flagyl without prescription
buy doxycycline without prescription
xanax generic
pharmacy propecia
online flagyl
buy cheap plavix
propecia prices
doxycycline for sale
zoloft prescription
cheap bactrim tablets
price of soma
prozac generic
soma prescription
lowest price plavix
generic cialis
clomid for sale
xanax discount
synthroid without prescription
buy doxycycline cheap
accutane discount
levitra online cheap
clomid prescription
phentermine prices
zoloft discount
xanax prescription
cheap doxycycline tablets
tramadol online cheap
discount lorazepam
cheap zoloft
order lorazepam
buy cheap clomid online
viagra online
buy levitra cheap
flagyl cheap
diazepam pharmacy
where to buy doxycycline
buy generic zithromax
flagyl online stores
buy cheap soma
where to buy diazepam
generic xanax
cheapest zithromax prices
phentermine without prescription
generic plavix
pharmacy doxycycline
valium online cheap
accutane no prescription
buy xanax
acomplia no prescription
price of phentermine
zithromax prescription
purchase flagyl
propecia discount
cheapest viagra
cheap tramadol
cheap clomid online
cheap synthroid online
lowest price phentermine
order nexium
generic tramadol
prozac online
buy generic doxycycline
price of doxycycline
purchase bactrim
cheap generic prozac
where to buy bactrim
buy phentermine online
buy zithromax
buy zoloft online
cheap levitra online
buy generic bactrim
buy xanax online
buy cialis without prescription
clomid pharmacy
buy lasix online
synthroid online
zoloft no prescription
cheap valium online
propecia for sale
buy prozac cheap
cheap generic phentermine
order bactrim
cialis no prescription
pharmacy bactrim
propecia online cheap
cheap diazepam online
propecia without prescription
nexium cheap
cheap zithromax online
discount valium
order tramadol
discount propecia
buy cheap viagra
order zithromax online
cheapest tramadol prices
plavix generic
accutane for sale
flagyl pharmacy
purchase lasix
cheap levitra tablets
order propecia
buy cheap doxycycline
diazepam online cheap
nexium
lowest price viagra
levitra online stores
cheap generic accutane
buy viagra cheap
price of xanax
acomplia prescription
cheapest soma
buy cheap zithromax
alprazolam for sale
cheap soma tablets
purchase doxycycline online
purchase cialis
buy cheap alprazolam online
pharmacy xanax
buy acomplia online
cheap levitra
plavix prescription
phentermine online cheap
cheapest lasix prices
cheap lorazepam tablets
bactrim for sale
phentermine online
pharmacy prozac
zoloft without prescription
buy valium
lasix generic
lasix discount
purchase diazepam online
xanax online stores
buy zithromax without prescription
purchase bactrim online
xanax sale
bactrim sale
cheapest doxycycline prices
cheap generic flagyl
xanax cheap
synthroid no prescription
buy synthroid without prescription
lorazepam sale
cheap alprazolam
buy phentermine
cheap doxycycline
cheap propecia
buy cheap zithromax online
cheapest nexium prices
lowest price synthroid
cheapest plavix
synthroid prescription
accutane generic
discount levitra
buy cheap lasix
buy synthroid cheap
cialis generic
cialis discount
cialis
pharmacy diazepam
xanax without prescription
where to buy levitra
nexium no prescription
cialis online stores
bactrim discount
discount soma
buy lasix without prescription
doxycycline online
cheap generic viagra
flagyl online
buy soma online
discount diazepam
cheap lorazepam online
lowest price bactrim
tramadol prices
cheapest phentermine
nexium prescription
cheapest doxycycline
clomid pills
alprazolam generic
buy acomplia without prescription
valium pills
clomid prices
viagra pills
flagyl online cheap
bactrim prescription
buy propecia without prescription
cheapest plavix prices
online phentermine
cheap plavix tablets
tramadol online
buy cheap viagra online
buy viagra
generic viagra
where to buy valium
valium online
buy acomplia
diazepam online stores
cheap generic zithromax
lorazepam prices
alprazolam
cheap valium tablets
accutane cheap
buy generic levitra
tramadol cheap
purchase zoloft
phentermine cheap
tramadol
online prozac
cheapest synthroid prices
zoloft sale
lowest price doxycycline
viagra without a prescription
buy soma
cheap generic nexium
cheap generic lasix
order nexium online
acomplia discount
flagyl no prescription
buy cialis online
synthroid
viagra for sale
where to buy prozac
valium sale
where to buy viagra
propecia online stores
purchase xanax
cialis online cheap
cheap plavix
viagra without prescription
purchase zithromax online
plavix pills
cheap prozac tablets
tramadol without prescription
lorazepam online stores
cialis without prescription
cheapest alprazolam
nexium online stores
purchase accutane
lorazepam no prescription
cheap acomplia tablets
buy cheap valium online
buy cheap valium
diazepam generic
order soma online
order zoloft online
zithromax discount
online diazepam
xanax pharmacy
buy generic valium
purchase nexium online
phentermine sale
cialis pharmacy
cheap cialis
buy zithromax online
purchase synthroid online
plavix no prescription
lowest price prozac
plavix discount
pharmacy valium
levitra pharmacy
prozac pills
flagyl sale
cheapest accutane
discount lasix
purchase soma online
generic lorazepam
cheap xanax tablets
lowest price lorazepam
accutane online cheap
bactrim prices
buy cheap levitra
purchase accutane online
order lasix online
bactrim generic
online zithromax
buy nexium cheap
lorazepam online cheap
soma online stores
propecia online
buy generic tramadol
nexium pharmacy
buy generic lasix
zoloft cheap
buy soma without prescription
levitra pills
acomplia online stores
zithromax generic
doxycycline sale
buy plavix cheap
lowest price levitra
cheap viagra online
price of levitra
where to buy acomplia
doxycycline generic
lasix no prescription
clomid discount
price of prozac
order flagyl online
flagyl generic
where to buy cialis
plavix for sale
buy generic zoloft
discount doxycycline
order synthroid online
buy accutane without prescription
buy cheap soma online
zithromax pills
purchase viagra online
purchase phentermine online
synthroid generic
synthroid pills
doxycycline without a prescription
zoloft online stores
buy viagra without prescription
plavix pharmacy
valium
clomid cheap
plavix sale
cheapest cialis
nexium pills
buy bactrim
phentermine generic
buy tramadol without prescription
buy accutane
xanax without a prescription
online soma
lasix for sale
buy generic synthroid
buy valium cheap
prozac without a prescription
cheapest clomid
order viagra online
alprazolam online cheap
buy cheap plavix online
cheapest alprazolam prices
online plavix
lasix prices
buy cheap bactrim online
buy bactrim online
buy lorazepam cheap
generic lasix
purchase cialis online
xanax online
buy lasix cheap
doxycycline online cheap
soma without prescription
lorazepam cheap
pharmacy phentermine
buy nexium without prescription
cheap propecia online
diazepam
order propecia online
discount flagyl
buy cheap lasix online
nexium online cheap
tramadol no prescription
where to buy soma
where to buy zoloft
cheap soma
lorazepam pills
cheap generic alprazolam
cheap generic tramadol
viagra discount
buy lorazepam online
accutane without prescription
lowest price tramadol
buy bactrim cheap
cheap soma online
online zoloft
zithromax no prescription
buy cheap prozac online
diazepam prices
online lasix
buy phentermine without prescription
clomid sale
doxycycline prices
buy alprazolam without prescription
buy cheap phentermine online
buy cheap levitra online
order acomplia
accutane sale
phentermine no prescription
bactrim without prescription
cheap tramadol tablets
zithromax prices
acomplia without prescription
levitra discount
lowest price propecia
lorazepam without prescription
buy cheap nexium online
buy cheap synthroid online
lowest price diazepam
discount viagra
price of alprazolam
zithromax pharmacy
buy cheap cialis online
online synthroid
buy diazepam
buy xanax cheap
cheapest propecia
order zithromax
generic acomplia
buy generic lorazepam
buy cheap clomid
cialis without a prescription
diazepam without a prescription
cheap generic valium
order prozac online
cheap generic doxycycline
pharmacy acomplia
buy generic prozac
price of valium
generic accutane
cheap diazepam
cheap synthroid tablets
phentermine prescription
order xanax online
cheap plavix online
buy cheap xanax
accutane prescription
generic prozac
purchase clomid
cheap generic xanax
buy lasix
cheap generic soma
lasix without prescription
pharmacy tramadol
cheap phentermine online
cheapest flagyl
alprazolam without a prescription
generic diazepam
valium pharmacy
valium for sale
order levitra online

I’ve been meaning to write this for a while. It started when I camed across this page of the shortest sudoku solvers. They all follow the basic algorithm, which is described here. I orginally thought it was a brute force algorithm, so I wanted a more elegant solution, but on closer examination it in fact uses constraints and backtracking, like a Prolog program would. Still I wanted to write one in Prolog. It turns out it’s quite simple, and here is my program:

% Sudoku Solver
%
% Author: Miles Barr
% Version: 1.0
%
% Example Usage:
%
% Board = [ [_,_,3,_,4,9,_,_,8],
%           [_,1,_,5,_,_,_,3,_],
%           [8,_,7,3,_,_,_,_,_],
%           [_,6,8,9,_,_,_,_,5],
%           [2,_,_,_,_,_,_,_,6],
%           [4,_,_,_,_,6,7,1,_],
%           [_,_,_,_,_,5,9,_,3],
%           [_,8,_,_,_,1,_,6,_],
%           [9,_,_,2,3,_,1,_,_]
%         ],
% sudoku(Board),
% print_board(Board).

:- use_module(library('clp/bounds')).
:- use_module(library('lists')).

% Our general constraint is that each set of 9 (row, column or 3x3 grid) is made
% up from the numbers 1-9, and each number is not repeated.
valid(L) :- L in 1..9, all_different(L).

% Transpose function from Worksheet 16 of 'Clause and Effect'
transpose([[]|_],[]).
transpose(R,[H|C]) :- chopcol(R,H,T), transpose(T,C).
chopcol([],[],[]).
chopcol([[H|T]|R],[H|Hs],[T|Ts]) :- chopcol(R,Hs,Ts).

% Describe 3 grids in terms of 3 rows
% TODO: There must be a better way to do this.
grids([E11,E12,E13,E14,E15,E16,E17,E18,E19],
      [E21,E22,E23,E24,E25,E26,E27,E28,E29],
      [E31,E32,E33,E34,E35,E36,E37,E38,E39],
      [E11,E12,E13,E21,E22,E23,E31,E32,E33],
      [E14,E15,E16,E24,E25,E26,E34,E35,E36],
      [E17,E18,E19,E27,E28,E29,E37,E38,E39]).

% Print a board
print_board([]).
print_board([H|T]) :- write(H), nl, print_board(T).

% Sudoku solving function. 
%
% Input is nine lists of nine integers
sudoku(Board) :-
  % Break the board down into rows
  Board = [R1, R2, R3, R4, R5, R6, R7, R8, R9],
  % Transpose the board to get the columns
  transpose(Board, [C1, C2, C3, C4, C5, C6, C7, C8, C9]),
  % Cut up the rows into 3x3 grids
  grids(R1,R2,R3,G1,G2,G3),
  grids(R4,R5,R6,G4,G5,G6),
  grids(R7,R8,R9,G7,G8,G9),
  % Each row contains unique entries
  valid(R1), valid(R2), valid(R3),
  valid(R4), valid(R5), valid(R6),
  valid(R7), valid(R8), valid(R9),
  % Each column contains unique entries
  valid(C1), valid(C2), valid(C3),
  valid(C4), valid(C5), valid(C6),
  valid(C7), valid(C8), valid(C9),
  % Each grid contains unique entries
  valid(G1), valid(G2), valid(G3),
  valid(G4), valid(G5), valid(G6),
  valid(G7), valid(G8), valid(G9),

  % Turn the board into a list of 81 variables
  flatten(Board, Elements),
  %sign a value to each variable
  label(Elements).

I used SWI Prolog, but it should be trivial to port to other versions.

The general constraint is described by the valid clause, each list must contain all the numbers 1 through 9 exactly once. The list can represent a row, column or 3×3 grid. The next bit is breaking up the board into rows, columns and 3×3 grids, and then making sure each one is valid, which is what the sudoku clause does. That’s it really, and that’s what my first version looked like. In fact my original valid clause was:

valid(L) :- permutation([1,2,3,4,5,6,7,8,9], L).

and I didn’t constraint the values and do a labelling. That didn’t work, or it might work, but it takes a very long time. I came across this Prolog solver, which reminded me of labelling, so I removed the use of permutation, the result, a near instantaneous solver. I also borrowed his printing function. :)

It’s much longer than the other versions, but it has the nice property that you describe the constraints, and that’s your program, you don’t need to implement backtracking. There are still improvements to be made here, the most important one would be to add in some sort of heuristic to guide the backtracking, at the moment it’s naive and problem does more work than it needs to.

But is this the best way to solve a Sudoku puzzle? One other idea I’ve started thinking about is representing the board as a graph. We have 81 squares, each with 9 values, so we start off with 729 nodes. Each node represents a possible value for that square. Each value node for that square is connected to all the other value nodes of the squares next to it. As we discover the value of certain squares we can mark arcs to other values as illegal, and not use them in our path. Once we have a legal path through one value node for each square, we’re done. Ultimately this method requires the use of the same constraints, so it probably won’t be much better than the list based solution, but it could allow multiple walkers to discover legal row, column or grid paths in parallel, eventually joining them up. This parallelisation might lead to a faster solution. But I’ll leave the implementation and analysis to another time.

Spread the word: Technorati related  |  Technorati related  |  del.icio.us bookmark it!  |  submit Prolog Sudoku Solver digg.com digg it!  |  reddit reddit!

5 Responses to “Prolog Sudoku Solver”

  1. The Gay Bar » Blog Archive » Sudoku solver in swi-prolog says:

    […] Well today on LarryTheCow I saw a post by Miles Barr who wrote a Sudoku solver in prolog. It kinda shows how simple that kind of program can be and how generally simple Sudokus are. I never really got the fascination many people have for Sudoku; it is a great task to write a solver (the problem is easy enough to be solved by starting programmers and non-trivial enough to make writing better solutions interesting for the geeks), but after doing one or two by hand I was kinda bored by it (I have done like 5 of them on my DS in Dr. Kawashima’s Brain Training but can’t bring myself to doing more of them). Technorati Tags: prolog, LarryTheCow, Sudoku solver, Sudoku […]

  2. anonymous says:

    Just try to integrate forward checking.
    Implement it, that every Field has a domain of {1,2,3,4,5,6,7,8,9} and whenever
    you set a Field’s value, you delay all inconsistent values from the domains of its “neighbours”
    (other Fields in the row/column and 3×3block).
    Sometimes considering the neighbours of the neighbours you has changed (recursiv) might be more efficient (not always)…
    Using this technic you may find wrong paths very fast and direct you search more directly to the solution.
    Another usefull heurstic for the Sudoku Problem to add, is selecting the field which will be selected next by the size of domain (select next Field by smallest Domainsize)…
    Together with depth-first search (until you don’t have any unset Fields) you can minimize backtracking and therefore eliminate states from your search graph before you have to create them.

    bye :)

  3. anonymous says:

    To the previous poster: Forward checking is done by the constraint library. Selecting the variable with smallest domain is done with labeling([ff], Vars) (available in library(bounds) in the latest SWI versions).

    Here’s a shorter version:

    :- use_module(library(bounds)).

    suudoku(Rows) :-
    Rows = [R1,R2,R3,R4,R5,R6,R7,R8,R9],
    flatten(Rows, Vars),
    Vars in 1..9,
    rowconstraint(Rows),
    column
    constraint(R1, R2, R3, R4, R5, R6, R7, R8, R9),
    blockconstraint(R1, R2, R3),
    block
    constraint(R4, R5, R6),
    blockconstraint(R7, R8, R9),
    label(Vars),
    display
    rows(Rows).

    displayrows([]).
    display
    rows([[X1,X2,X3,X4,X5,X6,X7,X8,X9]|Rows]) :-
    format(’~d ~d ~d ~d ~d ~d ~d ~d ~d \n’, [X1,X2,X3,X4,X5,X6,X7,X8,X9]),
    display_rows(Rows).

    rowconstraint([]).
    row
    constraint([R|Rt]) :- alldifferent(R), rowconstraint(Rt).

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

    blockconstraint([], [], []).
    block
    constraint([X1,X2,X3|R1], [X4,X5,X6|R2], [X7,X8,X9|R3]) :-
    alldifferent([X1,X2,X3,X4,X5,X6,X7,X8,X9]),
    block
    constraint(R1, R2, R3).

  4. Sigmundo Preissler Jr. says:

    Hello,

    You talked about the use of FC but omitted the role of ‘labeling’ above correct?

    Att

  5. Wally says:

    Hi all I really enjoy Sudoku and solving them, here is one basic looong code example. Might be some intersting coding though?!

    http://vbaexcel.eu/vba-macro-code/sudoku-solver
    http://vbaexcel.eu/vba-macro-code/sudoku-solver

Leave a Reply

You must be logged in to post a comment.


ati-tv software ABBYY FineReader 8.0 Professional Multilanguage
ipod nana software ABBYY FineReader 8.0 Professional Multilanguage
easy fax software ABBYY FineReader Professional Edition 9.0 with Djvu Addon
encoder coding software Ableton Live 6.0.9
analytical system software ACD Systems Combo Pack
employee scheduling software Acronis Disk Director Server 10.0
eraise fundraising software Acronis Disk Director Suite 10.0
openeye software Acronis Disk Editor v6.0.360
gateway limited software Acronis Drive Cleanser v6.0 Build 383
tungsten software download Acronis Migrate Easy Deluxe v1.0.0.43
physics software Acronis & Paragon Universal Boot CD USB 2009 1.0
avatar server software Acronis & Paragon Universal Boot CD USB 2009 1.0
imaging software driver Acronis & Paragon Universal Boot CD USB 2009 1.0
software raid performance Acronis PartitionExpert 2003
legislative trackinig software Acronis Privacy Expert Suite 7.0
software listings Acronis Privacy Expert Suite 7.0
art software program Acronis Recovery Expert Deluxe
adams software forms Acronis True Image 7.0
free reservations software Acronis True Image Echo Server for Windows 9.5
panda antiviruse software Acronis True Image Enterprise Server 9.1.3666
microsoft excel software Acronis True Image Home 11.0
tape duplication software Acronis True Image Workstation 9.1.3887
free dayplanner software ActionScript 3.0 in Flash CS3 Professional Essential Training
raft software ActiveState Komodo IDE 4.2.0
software crises ActiveState Perl Dev Kit Pro 7
jq-210 download software Adobe Acrobat 7 Professional for Mac
audit software tracking Adobe Acrobat 7 Professional for Mac
smartist software Adobe Acrobat 7.0 Professional
callender software Adobe Acrobat 7.0 Professional
currency conversion software Adobe Acrobat 8.0 Professional
n95 remove software Adobe Acrobat 8.0 Professional
anderson software internet Adobe Acrobat 8.0 Professional
genealogy publishing software Adobe Acrobat 8.0 Professional for Mac
article distribution software Adobe Acrobat 8.0 Professional for Mac
map maker software Adobe Acrobat 9 Pro Extended
decisionbar software Adobe Acrobat 9 Pro Extended
timekeeping system software Adobe Acrobat 9 Pro Extended
knowledge mapping software Adobe Acrobat V 6.0 Professional PC
writing style software Adobe Acrobat V 6.0 Professional PC
management product software Adobe After Effects 6.5 for Mac
military packaging software Adobe After Effects 6.5 for Mac
knockout software Adobe After Effects 6.5 for Mac
mantel test software Adobe After Effects 7.0 Standard
pst repair software Adobe After Effects 7.0 Standard
pcr software customers Adobe After Effects 7.0 Standard
loopholes software testing Adobe After Effects CS3
ebay software tools Adobe After Effects CS3
volleyball statistic software Adobe After Effects CS3
autoquote software Adobe Atmosphere 1.0
medical documentation software Adobe Audition 2.0
software distrubitor Adobe Audition 2.0
6600 software program Adobe Audition 3.0
buy financial software Adobe Contribute CS3
apartment purchasing software Adobe Contribute CS3
define software application Adobe Creative Suite 2 Premium for Mac
autocad software review Adobe Creative Suite 2 Premium for Mac
pctools software Adobe Creative Suite 2 Premium for Mac
webchat software Adobe Creative Suite 2 Premium for Windows
book software store Adobe Creative Suite 2 Premium for Windows
software remote access Adobe Creative Suite 2 Premium for Windows
aor scanner software Adobe Creative Suite 2 Premium for Windows
software design article Adobe Creative Suite 3 Design Premium for Mac
microstudio 4.001 software Adobe Creative Suite 3 Design Premium for Mac
netscape 8 software Adobe Creative Suite 3 Design Premium for Mac
biblesoft software update Adobe Creative Suite 3 Design Premium for Mac
linux frontbridge software Adobe Creative Suite 3 Design Premium for Mac
code repository software Adobe Creative Suite 3 Design Premium for Win
acceleration software program Adobe Creative Suite 3 Design Premium for Win
clinical database software Adobe Creative Suite 3 Design Premium for Win
midi studio software Adobe Creative Suite 3 Design Premium for Win
aqura software Adobe Creative Suite 3 Master Collection for Mac
ezdata software Adobe Creative Suite 3 Master Collection for Mac
brightstore backup software Adobe Creative Suite 3 Master Collection for Mac
tk20 software Adobe Creative Suite 3 Master Collection for Mac
sequence analysis software Adobe Creative Suite 3 Master Collection for Mac
deployable spy software Adobe Creative Suite 3 Master Collection for Win
dprofiler software Adobe Creative Suite 3 Master Collection for Win
atm switch software Adobe Creative Suite 3 Master Collection for Win
hyperion software operations Adobe Creative Suite 3 Master Collection for Win
voice synthesis software Adobe Creative Suite 3 Master Collection for Win
iomega ghost software Adobe Creative Suite 3 Master Collection for Win
ups calculation software Adobe Creative Suite for Mac
oracle software Adobe Creative Suite for Mac
discount cad software Adobe Creative Suite for Mac
audio software forum Adobe Creative Suite for Mac
architectural renovation software Adobe Creative Suite 3 Master Collection for Win + Microsoft Office 2007 Enterprise
iphone 1.0.2 software Adobe Dreamweaver CS3
edsoft software Adobe Dreamweaver CS3
swing analysis software Adobe Dreamweaver CS3 for Mac
flowsheet software Adobe Encore CS3
lunix old software Adobe Encore CS3
software download drums Adobe Encore CS3
ficiton software Adobe Encore DVD 2.0
ebay software bidding Adobe Encore DVD 2.0
src software incorporated Adobe Encore DVD 2.0
amateur radio software Adobe Fireworks CS3
coin collectors software Adobe Fireworks CS3 for Mac
software package Adobe Flash CS3 Professional
convention software Adobe Flash CS3 Professional
picture software free Adobe Flash CS3 Professional for Mac
software circuit design Adobe Flash CS3 Professional for Mac
lenticular software crack Adobe Flash CS4
journal software engineering Adobe Flash CS4
webpage editor software Adobe Flash CS4
airframe business software Adobe Flex v.3.0.2
delmia software Adobe Flex v.3.0.2
freehand graphics software Adobe Font Folio 11
northrop resume software Adobe Font Folio 11
diabetes software pdas Adobe FrameMaker 7.0
istante software inc Adobe FrameMaker 7.0
vigilance software Adobe FrameMaker 8.0
software kinder preschool Adobe FrameMaker 8.0
dex drive software Adobe FrameMaker 9.0
de elementos software Adobe FrameMaker 9.0
software inspections Adobe FrameMaker 9.0
free cdrw software Adobe GoLive CS V 7.0 PC
ftp p2p software Adobe GoLive CS V 7.0 PC
transmit software Adobe GoLive CS2
becker data software Adobe GoLive CS2
fta firmware software Adobe Illustrator CS V 11.0 PC
liquids marketing software Adobe Illustrator CS V 11.0 PC
motorola v-180 software Adobe Illustrator CS2
explore anywhere software Adobe Illustrator CS3
outsourcing software solution Adobe InDesign CS V 3.0 PC
wntipcfg software Adobe InDesign CS V 3.0 PC
opengl software program Adobe InDesign CS2
nokia 6102i software Adobe InDesign CS2
free coloring software Adobe InDesign CS3
opp blackjack software Adobe InDesign CS3
astronomie software free Adobe InDesign CS4
monitoring email software Adobe InDesign CS4
development proposal software Adobe InDesign CS4
software cpu underclocking Adobe PageMaker 7.0.1
feko software Adobe Photoshop Album V 2.0
educational software seattle Adobe Photoshop Album V 2.0
learning measurement software Adobe Photoshop CS for Mac
flir software Adobe Photoshop CS for Mac
gps tracks software Adobe Photoshop CS v.8.0
free donation software Adobe Photoshop CS v.8.0
software updating freeware Adobe Photoshop CS2 for Mac
mountbridge software Adobe Photoshop CS2 for Mac
scream software seismic Adobe Photoshop CS2 V 9.0
software requirements plan Adobe Photoshop CS2 V 9.0
mpeg4 slideshow software Adobe Photoshop CS2 V 9.0
french genealogy software Adobe Photoshop CS3: Enhancing Digital Photographs
free software imac Adobe Photoshop CS3: Enhancing Digital Photographs
graphics benchmarking software Adobe Photoshop CS3 Extended
firewall software features Adobe Photoshop CS3 Extended
automatic prayer software Adobe Photoshop CS3 Extended
pine software examples Adobe Photoshop CS3 Extended for Mac