|
1
|
- Chapter 6 introduces the stack data type.
- Several example applications of stacks are given in that chapter.
- This presentation shows another use called backtracking to solve the
N-Queens problem.
|
|
2
|
- Suppose you have 8 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 allowed in the same row...
|
|
5
|
- Two queens are not allowed in the same
row, or in the same column...
|
|
6
|
- Two queens are not allowed in the same
row, or in the same column, or along the same diagonal.
|
|
7
|
- The number of queens, and the size of the board can vary.
|
|
8
|
- We will write a program which tries to find a way to place N queens on
an N x N chess board.
|
|
9
|
- The program uses a stack to keep track of where each queen is placed.
|
|
10
|
- Each time the program decides to place a queen on the board, the position of the new queen is
stored in a record which is placed in the stack.
|
|
11
|
- We also have an integer variable to keep track of how many rows have
been filled so far.
|
|
12
|
- Each time we try to place a new queen in the next row, we start by
placing the queen in the first column...
|
|
13
|
- ...if there is a conflict with another queen, then we shift the new
queen to the next column.
|
|
14
|
- If another conflict occurs, the queen is shifted rightward again.
|
|
15
|
- When there are no conflicts, we stop and add one to the value of filled.
|
|
16
|
- Let's look at the third row. The
first position we try has a conflict...
|
|
17
|
- ...so we shift to column 2. But
another conflict arises...
|
|
18
|
- ...and we shift to the third column.
- Yet another conflict arises...
|
|
19
|
- ...and we shift to column 4.
There's still a conflict in column 4, so we try to shift
rightward again...
|
|
20
|
- ...but there's nowhere else to go.
|
|
21
|
- When we run out of
- room in a row:
- pop the stack,
- reduce filled by 1
- and continue
working on the previous row.
|
|
22
|
- Now we continue working on row 2, shifting the queen to the right.
|
|
23
|
- This position has no conflicts, so we can increase filled by 1, and move
to row 3.
|
|
24
|
- In row 3, we start again at the first column.
|
|
25
|
- Initialize a stack where we can keep track of our decisions.
- Place the first queen, pushing its position onto the stack and setting filled
to 0.
- repeat these steps
- if there are no conflicts with the queens...
- else if there is a conflict and there is room to shift the current
queen rightward...
- else if there is a conflict and there is no room to shift the current
queen rightward...
|
|
26
|
- repeat these steps
- if there are no conflicts with the queens...
|
|
27
|
- repeat these steps
- if there are no conflicts with the queens...
- else if there is a conflict and there is room to shift the current
queen rightward...
|
|
28
|
- repeat these steps
- if there are no conflicts with the queens...
- else if there is a conflict and there is room to shift the current
queen rightward...
- else if there is a conflict and there is no room to shift the current
queen rightward...
|
|
29
|
- repeat these steps
- if there are no conflicts with the queens...
- else if there is a conflict and there is room to shift the current
queen rightward...
- else if there is a conflict and there is no room to shift the current
queen rightward...
|
|
30
|
- You can double click the left mouse button here to run the demonstration
program a second time:
|
|
31
|
- Stacks have many applications.
- The application which we have shown is called backtracking.
- The key to backtracking: Each choice is recorded in a stack.
- When you run out of choices for the current decision, you pop the stack,
and continue trying different choices for the previous decision.
|
|
32
|
|