Each time the Each time the program decides program decides to place a queen to place a queen on the board, on the board, the position of the position of the new qu[r]
(1)Chapter 7 introduces the
Chapter 7 introduces the
stack
stack data type.data type Several example Several example applications of stacks are applications of stacks are given in that chapter. given in that chapter. This presentation shows This presentation shows another use called another use called backtracking to solve the backtracking to solve the NQueens problem
NQueens problem
(2)Suppose you have 8
Suppose you have 8
chess queens
chess queens
and a chess board
(3)Can the queens be
placed on the board so that no two queens are attacking each other
?
(4)Two queens are not
Two queens are not
allowed in the same
allowed in the same
row
(5)Two queens are not
Two queens are not
allowed in the same
allowed in the same
row, or in the same
row, or in the same
column
(6)Two queens are not
Two queens are not
allowed in the same
allowed in the same
row, or in the same
row, or in the same
column, or along the
column, or along the
same diagonal
(7)The number of queens,
The number of queens,
and the size of the board
and the size of the board
can vary
can vary
N ro ws
(8)We will write a program
We will write a program
which tries to find a way
which tries to find a way
to place N queens on an
to place N queens on an
N
(9)The program
The program
uses a stack to
uses a stack to
keep track of
keep track of
where each
where each
queen is placed
(10)