8 queens 5

Description

Program solves 8 queens problem

Source: PROLOG programming for artificial intelligence, 3rd Edition, Harlow, 2001, ISBN 0-201-40375-7.

Download

Program source code: 8_queens_5.pl

Listing

% Figure 4.11   Program 3 for the eight queens problem.
 
 
% solution( Ylist) if 
%   Ylist is a list of Y-coordinates of eight non-attacking queens
 
solution( Ylist)  :-   
  sol( Ylist,                                    % Y-coordinates of queens
       [1,2,3,4,5,6,7,8],                        % Domain for X-coordinates
       [1,2,3,4,5,6,7,8],                        % Domain for Y-coordinates
       [-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7],   % Upward diagonals
       [2,3,4,5,6,7,8,9,10,11,12,13,14,15,16] ). % Downward diagonals 
 
sol( [], [], Dy, Du, Dv).
 
sol( [Y | Ylist], [X | Dx1], Dy, Du, Dv)  :-
  del( Y, Dy, Dy1),                 % Choose a Y-coordinate
  U is X-Y,                         % Corresponding upward diagonal
  del( U, Du, Du1),                 % Remove it
  V is X+Y,                         % Corresponding downward diagonal
  del( V, Dv, Dv1),                 % Remove it
  sol( Ylist, Dx1, Dy1, Du1, Dv1).  % Use remaining values 
 
del( Item, [Item | List], List).
 
del( Item, [First | List], [First | List1] )  :-
   del( Item, List, List1).

Comments

pl/prolog/pllib/8_queens_5.txt · ostatnio zmienione: 2017/07/17 08:08 (edycja zewnętrzna)
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0