Notes
Slide Show
Outline
1
Using a Stack
  • 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
The N-Queens Problem
  • Suppose you have 8 chess queens...
  • ...and a chess board
3
The N-Queens Problem
  • Can the queens be placed on the board so that no two queens are attacking each other
4
The N-Queens Problem
  • Two queens are not allowed in the same row...
5
The N-Queens Problem
  • Two queens are not allowed in the same  row, or in the same column...
6
The N-Queens Problem
  • Two queens are not allowed in the same  row, or in the same column, or along the same diagonal.
7
The N-Queens Problem
  • The number of queens, and the size of the board can vary.
8
The N-Queens Problem
  • We will write a program which tries to find a way to place N queens on an N x N chess board.
9
How the program works
  • The program uses a stack to keep track of where each queen is placed.
10
How the program works
  • 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
How the program works
  • We also have an integer variable to keep track of how many rows have been filled so far.
12
How the program works
  • Each time we try to place a new queen in the next row, we start by placing the queen in the first column...
13
How the program works
  • ...if there is a conflict with another queen, then we shift the new queen to the next column.
14
How the program works
  • If another conflict occurs, the queen is shifted rightward again.
15
How the program works
  • When there are no conflicts, we stop and add one to the value of filled.
16
How the program works
  • Let's look at the third row.  The first position we try has a conflict...
17
How the program works
  • ...so we shift to column 2.  But another conflict arises...
18
How the program works
  • ...and we shift to the third column.
  • Yet another conflict arises...
19
How the program works
  • ...and we shift to column 4.  There's still a conflict in column 4, so we try to shift rightward again...
20
How the program works
  • ...but there's nowhere else to go.
21
How the program works
  • When we run out of
  • room in a row:
  • pop the stack,
  • reduce filled by 1
  • and continue                       working on the previous row.
22
How the program works
  • Now we continue working on row 2, shifting the queen to the right.
23
How the program works
  • This position has no conflicts, so we can increase filled by 1, and move to row 3.
24
How the program works
  • In row 3, we start again at the first column.
25
Pseudocode for N-Queens
  • 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
Pseudocode for N-Queens
  • repeat these steps
    • if there are no conflicts with the queens...
27
Pseudocode for N-Queens
  • 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
Pseudocode for N-Queens
  • 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
Pseudocode for N-Queens
  • 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
Watching the program work
  • You can double click the left mouse button here to run the demonstration program a second time:
31
   Summary
  • 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
THE  END