1 / 33

The N-Queens Problem

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.

kolton
Download Presentation

The N-Queens Problem

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The N-Queens Problem lab01

  2. 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.

  3. Attacking of queens • Two queens are attacking each other if they are • in the same row • in the same column • in the same diagonal

  4. Denoting the positions of queens • A 4 by 4 chess board. (4 rows, 4 columns) row 3, column 1 row 2, column 3 row 1, column 1

  5. A 8 by 8 chess board. (8 rows, 8 columns) a8 b4

  6. Pseudocodeof N-queens Problem • Initialize a stack s where we can keep track of the placement of queens • Place the first queen, push its position onto s and set filled to 0. • int filled =0;

  7. Continued… increase 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. • 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 rightward 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.

  8. Example: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 top Stack s bottom

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

  10. there are no conflicts, increase filled by 1.

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

  12. 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).

  13. 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 3

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

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

  16. 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 2)

  17. 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)

  18. 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)

  19. 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 4

  20. there 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).

  21. 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 1 ROW 3, COL 2

  22. there 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 4, COL 1 ROW 3, COL 2 ROW 3, COL 2 3 3

  23. 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 4, COL 1 ROW 4, COL 2 ROW 3, COL 2 ROW 3, COL 2 3 3

  24. 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 4, COL 2 ROW 4, COL 3 ROW 3, COL 2 ROW 3, COL 2 3 3

  25. ROW 4, COL 3 ROW 4, COL 4 ROW 3, COL 2 ROW 3, COL 2 3 3

  26. 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 , COL ) ROW 4, COL 4 ROW 3, COL 2 ROW 3, COL 3 3 2

  27. ROW 3, COL 3 ROW 3, COL 4 2 2

  28. 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 , COL ) ROW 2, COL 4 ROW 2, COL 4 ROW 1, COL 1 2 1=filled

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

  30. Two steps are omitted here. ROW 2, COL 4 ROW 2, COL 1 ROW 1, COL 2 ROW 1, COL 2 2=filled 1=filled

  31. ROW 3, COL 1 ROW 2, COL 4 ROW 1, COL 2 3=filled

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

More Related