Practical No: 13: To Write A Prolog - Program For N Queen Problem
Practical No: 13: To Write A Prolog - Program For N Queen Problem
CLASS : BE-B
ROLL NO : 243
BATCH : B3
GRADE :
SIGN :
AIM: To Write a prolog -program for n queen problem
THEORY:
The eight queens puzzle is the problem of placing eight chess queens on an 88 chessboard so
that no two queens threaten each other. Thus, a solution requires that no two queens share the
same row, column, or diagonal. The eight queens puzzle is an example of the more general n-
queens problem of placing n queens on an nn chessboard, where solutions exist for all natural
numbers nwith the exception of n=2 and n=3.
The problem can be quite computationally expensive as there are 4,426,165,368 (i.e., 64C8)
possible arrangements of eight queens on an 88 board, but only 92 solutions. It is possible to
use shortcuts that reduce computational requirements or rules of thumb that avoids brute-force
computational techniques. For example, just by applying a simple rule that constrains each queen
to a single column (or row), though still considered brute force, it is possible to reduce the
number of possibilities to just 16,777,216 (that is, 88) possible combinations.
Generating permutations further reduces the possibilities to just 40,320 (that is, 8!), which are
then checked for diagonal attacks.In the N Queens problem, the object is to place N queens on a
chessboard in such a way that no two queens can take each other. Accordingly, no two queens
can be placed on the same row, column, or diagonal.
To solve the problem, you'll number the rows and columns of the chessboard from 1 to N. To
number the diagonals, you divide them into two types, so that a diagonal is uniquely specified by
a type and a number calculated from its row and column numbers:
When you view the chessboard with row 1 at the top and column 1 on the left side, Type 1
diagonals resemble the backslash () character in shape, and Type 2 diagonals resemble the
shape of slash (/). Figure 16.5 shows the numbering of Type 2 diagonals on a 4x4 board.
Fig: The N-Queens Chess Board(4*4)
PROGRAM:
DOMAINS
queens = queen*
freelist = integer*
PREDICATES
nondeterm nqueens(integer)
CLAUSES
nqueens(N):-
makelist(N,L),Diagonal=N*2-1,makelist(Diagonal,LL),
placeN(N,board([],L,L,LL,LL),Final), write(Final).
placeN(_,board(D,[],[],D1,D2),board(D,[],[],D1,D2)):-!.
placeN(N,Board1,Result):-
place_a_queen(N,Board1,Board2),
placeN(N,Board2,Result).
place_a_queen(N,board(Queens,Rows,Columns,Diag1,Diag2),
board([q(R,C)|Queens],NewR,NewC,NewD1,NewD2)):-
nextrow(R,Rows,NewR),
findandremove(C,Columns,NewC),
D1=N+C-R,findandremove(D1,Diag1,NewD1),
D2=R+C-1,findandremove(D2,Diag2,NewD2).
findandremove(X,[X|Rest],Rest).
findandremove(X,[Y|Rest],[Y|Tail]):-
findandremove(X,Rest,Tail).
makelist(1,[1]).
makelist(N,[N|Rest]) :-
N1=N-1,makelist(N1,Rest).
nextrow(Row,[Row|Rest],Rest)
OUTPUT:
N = 6,
L = [1/5, 2/3, 3/1, 4/6, 5/4, 6/2];
N = 6,
L = [1/4, 2/1, 3/5, 4/2, 5/6, 6/3];
N = 6,
L = [1/3, 2/6, 3/2, 4/5, 5/1, 6/4];
N = 6,
L = [1/2, 2/4, 3/6, 4/1, 5/3, 6/5];