0% found this document useful (0 votes)
410 views13 pages

5 Queens PROBLEM

FAI NOTES

Uploaded by

reshma.r
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
410 views13 pages

5 Queens PROBLEM

FAI NOTES

Uploaded by

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

The N-Queens

Problem RESHMA R
ASSISTANT PROFESSOR
CSE, KAHE
N-queens problem
Find a configuration of n queens not attacking each other
1. The maximum number of queens that can be placed on an N x N
chessboard such that no two attack one another- N queens,
2. Example:- 8 queens for the usual 8x8 board
12 Unique Solutions
8-Queens Problem
8-Queens Problem
There are 92 distinct solutions
There are 12 unique solutions discounting
symmetrical answers (rotations/reflections)

How many solutions for 4-Queens? N-Queens?


Problems N < 4
N<4
Cannot use N Queens
3

1
The minimum number of queens needed to occupy or attack all squares
of an 8x8 board
states? -any arrangement of n<=8 queens
-or arrangements of n<=8 queens in leftmost n
columns, 1 per column, such that no queen
attacks any other.
initial state? no queens on the board
actions? -add queen to any empty square
-or add queen to leftmost empty square such
that it is not attacked by other queens.
goal test? 8 queens on the board, none attacked.
path cost? 1 per move
A Solution: Know your
domain
1. Since there are N rows and N queens, there must be a queen in
each column

2. Put a queen in each row, but also need to pick a row for each
queen.

3. Randomize the rows at the beginning, and then, in each iteration


move one queen that reduces the number of threatened queens by
a maximum.

4. We do it in each iteration, until the number if threatened queens


reach 0, or we get to a situation where we can’t improve any more,
because we had a bad start - so we start over, randomizing locations
again, and doing the whole thing again, until the queens a safe spot.
Solution: Backtracking
1. Place a queen in the top row, then note the column and diagonals it
occupies.

2. Place a queen in the next row down, taking care not to place it in the same
column or diagonal. Keep track of the occupied columns and diagonals
and move on to the next row.

3. If no position is open in the next row, we back track to the previous row
and move the queen over to the next available spot in its row and the
process starts over again.
Computational
Considerations
Computer solutions to the N-Queens problem are basically the same as the
method you would use by hand.

It is simply a brute force trial and error method.

The amount of time required to find all solutions for any order “N” is roughly proportional to “N”
Factorial.

It took over 11 days to get the results for “N” = 20.

If we increase “N” to 21, it would take about 4 months for the program to run.

For higher orders of “N”, the problem has to be broken into parts with each part delegated to a
separate computer. Thus, dozens and more likely, hundreds of computers are needed to solve
problems with “N” in the low 20's. With present computing power, it is unlikely that the total
number of solutions will be found when “N” equals 30 or higher.
THANK YOU!!!

You might also like