Вы находитесь на странице: 1из 4

NQueen’s Problem Using

Backtracking

For

Knowledge Based Computer System

Under the Guidance of

Prof. Vinay Kumar

Submitted by

Ashwath A Biradar (10IS05F)

Department of Computer Engineering


National Institute of Technology, Surathkal
Karnataka
NQueen’s Problem

Abstract:

The backtracking method is based on the systematically inquisition of the possible solutions
where through the procedure, set of possible solutions are rejected before even examined
so their number is getting a lot smaller.

An important requirement which must be fulfilled is that there must be the proper hierarchy
in the systematically produce of solutions so that sets of solutions that do not fulfil a certain
requirement are rejected before the solutions are produced. For this reason the
examination and generations of the solutions follows a model of non-cycle graph for which
in this case we will consider as a tree. The root of the tree represents the set of all the
solutions. Nodes in lower levels represent even smaller sets of solutions, based on their
properties. Obviously, leaves will be isolated solutions. It is easily understood that the tree
(or any other graph) is produced during the examination of the solutions so that no rejected
solutions are produced. When a node is rejected, the whole sub-tree is rejected, and we
backtrack to the ancestor of the node so that more children are produced and examined.
Because this method is expected to produce subsets of solutions which are difficult to
process, the method itself is not very popular.

Problem Statement:

Given is a chess board. A chess board has 8x8 fields. Is it possible to place 8 queens on this
board, so that no two queens can attack each other?

NQueen’s problem is a problem of placing chess queen’s in a square matrix. Example: for 4
queen’s problem, we need to place 4 queen’s in a 4 X 4 matrix so that no two queen’s attack
each other. I.e. solution requires that no two queen’s share the same row, column, or
diagonal. The solution exists only for n=1 and n>=4.
Solution:

The algorithm to solve this problem uses backtracking. The basic idea is to place queens row
by row, starting at the top. New queens must not be attacked by the ones to the top that
have already been placed on the board. We place another queen in the next row if a
consistent position is found.

We consider a grid of squares, dimensioned n X n, partly equivalent to a chessboard


containing n^2 places. A queen placed in any of the n^2 squares controls all the squares that
are on its row, its column and the diagonals. The problem asked, is how to put n queens on
the chessboard, so that the square of every queen is not controlled by any other queen.
Obviously for n=2 there is no problem to the solution, while for n=4 a valid solution is given
by the drawing below.

Basic idea of solution:

 Start with one queen in the first column, first row.


 Start with another queen in the second column, first row.
 Go down with the second queen until you reach a permissible situation.
 Advance to the next column, first row, and do the same thing.
 If you cannot find a permissible situation in one column and reach the bottom of it,
then you have to go back to the previous column and move one position down
there. (This is the backtracking step.)
 If you reach a permissible situation in the last column of the board, then the problem
is solved.
 If you have to backtrack BEFORE the first column, then the problem is not solvable.
Snapshots of Outputs: For the n value 4

References:

 T H Coreman, Introduction to Algorithms


 www.en.wikipedia.org/wiki/Eight_queens_puzzle

Вам также может понравиться