33
The N-Queens Problem lab01

The N-Queens Problem

  • Upload
    kolton

  • View
    54

  • Download
    1

Embed Size (px)

DESCRIPTION

The N-Queens Problem. lab01. The N-Queens Problem. Suppose you have N chess queens… …and a N by N chess board Can the queens be placed on board without conflicts? Conflict: Any two queens on board attacking each other is a conflict. A ttacking of queens. - PowerPoint PPT Presentation

Citation preview

The N-Queens Problem

The N-Queens Problemlab01The N-Queens ProblemSuppose you have N chess queensand a N by N chess boardCan the queens be placed on board without conflicts?Conflict: Any two queens on board attacking each other is a conflict.

Attacking of queensTwo queens are attacking each other if they are in the same rowin the same columnin the same diagonalDenoting the positions of queensA 4 by 4 chess board. (4 rows, 4 columns)

row 1, column 1row 2, column 3row 3, column 1A 8 by 8 chess board. (8 rows, 8 columns)

b4a8Pseudocode of N-queens ProblemInitialize a stack s where we can keep track of the placement of queensPlace the first queen, push its position onto s and set filled to 0.int filled =0;Repeat these steps:if there are no conflicts

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 rightwardincrease filled by 1. If filled is now N, then algorithm is done. Otherwise move to the next row and place a queen in the first column. Push its position onto the stack.Move the current queen rightward, adjusting the record on top of stack to indicate the new position.Keep popping the stack, and decrease filled by 1 until a row is reached where the queen can be shifted rightward. Shift the queen rightward and adjust the record on top of stack to indicate the new position.ContinuedExample:4-queues problem(4 queens on a 4 by 4 board)Initialize a stack s where we can keep track of the placement of queens

Stack stopbottom

Place the first queen, push its position onto s and set filled to 0. int filled =0;

there are no conflicts, increase filled by 1.

move to the next row (row 2) and place a queen in the first column. Push its position onto the stack.

If there is a conflict (yellow line), then we shift the new queen to the next column. (rightward ). Adjust the record on top of stack to indicate the new position (ROW 2, COL 2).

If there is a conflict (yellow line), then we shift the new queen to the next column. (rightward ). Adjust the record on top of stack to indicate the new position (ROW 2, COL 3).

ROW 2, COL 3there are no conflicts, increase filled by 1.

ROW 2, COL 3move to the next row and place a queen in the first column. Push its position onto the stack.

ROW 3, COL 1there is a conflict and there is room to shift the current queen rightward. Move the current queen rightward, adjusting the record on top of stack to indicate the new position. (ROW 2, COL 2)

there is a conflict and there is room to shift the current queen rightward. Move the current queen rightward, adjusting the record on top of stack to indicate the new position. (ROW 2, COL 3)

there is a conflict and there is room to shift the current queen rightward. Move the current queen rightward, adjusting the record on top of stack to indicate the new position. (ROW 2, COL 4)

there is a conflict and there is no room to shift the current queen rightward. Keep popping the stack, and decrease filled by 1 until a row is reached where the queen can be shifted rightward. Shift the queen rightward. Adjust the record on top of stack to indicate the new position (ROW 2, COL 4).

ROW 2, COL 4there are no conflicts, increase filled by 1 move to the next row and place a queen in the first column. Push its position onto the stack (ROW2, COL 1).

there is a conflict and there is room to shift the current queen rightward. Move the current queen rightward, adjusting the record on top of stack to indicate the new position.

ROW 3, COL 2ROW 3, COL 1there are no conflicts. Increase filled by 1. move to the next row and place a queen in the first column. Push its position onto the stack.

ROW 3, COL 23

ROW 3, COL 23

ROW 4, COL 1there is a conflict and there is room to shift the current queen rightward.Move the current queen rightward, adjusting the record on top of stack to indicate the new position.

ROW 3, COL 23

ROW 4, COL 2

ROW 3, COL 23

ROW 4, COL 1there is a conflict and there is room to shift the current queen rightward.Move the current queen rightward, adjusting the record on top of stack to indicate the new position.

ROW 3, COL 23

ROW 4, COL 3

ROW 3, COL 23

ROW 4, COL 2

ROW 3, COL 23

ROW 4, COL 4

ROW 3, COL 23

ROW 4, COL 3

ROW 3, COL 32

ROW 3, COL 23

ROW 4, COL 4there is a conflict and there is no room to shift the current queen rightwardKeep popping the stack, and decrease filled by 1 until a row is reached where the queen can be shifted rightward. Shift the queen rightward. Adjust the record on top of stack to indicate the new position (ROW , COL )

ROW 3, COL 42

ROW 3, COL 32there is a conflict and there is no room to shift the current queen rightwardKeep popping the stack, and decrease filled by 1 until a row is reached where the queen can be shifted rightward. Shift the queen rightward. Adjust the record on top of stack to indicate the new position (ROW , COL )

ROW 2, COL 42

ROW 1, COL 1ROW 2, COL 4

1=filledKeep popping the stack, because current queen can not move rightward. decrease filled by 1Shift the queen rightward. Adjust the record on top of stack to indicate the new position (ROW 1 , COL 2).

ROW 1, COL 1ROW 2, COL 4

ROW 1, COL 2

1=filled0=filled

ROW 1, COL 2

1=filled

ROW 2, COL 1

ROW 1, COL 2

2=filled

ROW 2, COL 4Two steps are omitted here.

ROW 1, COL 2

3=filled

ROW 2, COL 4

ROW 3, COL 1

ROW 1, COL 2

4=filled

ROW 2, COL 4

ROW 3, COL 1

ROW 4, COL 3if there are no conflicts increase filled by 1. If filled is now N(N=4), then algorithm is done.