0% found this document useful (1 vote)
681 views5 pages

Practical No: 13: To Write A Prolog - Program For N Queen Problem

The document describes a Prolog program to solve the N Queens problem of placing N queens on an N x N chessboard so that no two queens attack each other. It provides the program domains, predicates, and clauses to recursively place queens on the board one by one, checking that each placement is valid by not being in the same row, column or diagonal as another placed queen. The program outputs the solutions to the 6 Queens problem placement on a 6x6 board.

Uploaded by

Kumar Ubale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
681 views5 pages

Practical No: 13: To Write A Prolog - Program For N Queen Problem

The document describes a Prolog program to solve the N Queens problem of placing N queens on an N x N chessboard so that no two queens attack each other. It provides the program domains, predicates, and clauses to recursively place queens on the board one by one, checking that each placement is valid by not being in the same row, column or diagonal as another placed queen. The program outputs the solutions to the 6 Queens problem placement on a 6x6 board.

Uploaded by

Kumar Ubale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

PRACTICAL NO: 13

AIM : To Write a prolog -program for n queen problem

NAME : Kalpesh V Thakare

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:

Diagonal = N + Column - Row (Type 1)


Diagonal = Row + Column - 1 (Type 2)

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

queen = q(integer, integer)

queens = queen*

freelist = integer*

board = board(queens, freelist, freelist, freelist, freelist)

PREDICATES

nondeterm placeN(integer, board, board)

nondeterm place_a_queen(integer, board, board)

nondeterm nqueens(integer)

nondeterm makelist(integer, freelist)

nondeterm findandremove(integer, freelist, freelist)

nextrow(integer, freelist, freelist)

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];

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy