Lab01. The N-Queens Problem Suppose you have N chess queens… …and a N by N chess board Can the...
-
Upload
augustine-patrick -
Category
Documents
-
view
216 -
download
1
Transcript of Lab01. The N-Queens Problem Suppose you have N chess queens… …and a N by N chess board Can the...
![Page 1: 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:](https://reader038.fdocuments.us/reader038/viewer/2022102818/56649cdb5503460f949a6705/html5/thumbnails/1.jpg)
The N-Queens Problemlab01
![Page 2: 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:](https://reader038.fdocuments.us/reader038/viewer/2022102818/56649cdb5503460f949a6705/html5/thumbnails/2.jpg)
The N-Queens ProblemSuppose you have N chess queens……and 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.
![Page 3: 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:](https://reader038.fdocuments.us/reader038/viewer/2022102818/56649cdb5503460f949a6705/html5/thumbnails/3.jpg)
Attacking of queensTwo queens are attacking each other if they
are in the same rowin the same columnin the same diagonal
![Page 4: 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:](https://reader038.fdocuments.us/reader038/viewer/2022102818/56649cdb5503460f949a6705/html5/thumbnails/4.jpg)
Denoting the positions of queensA 4 by 4 chess board. (4 rows, 4 columns)
row 1, column 1
row 2, column 3
row 3, column 1
![Page 5: 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:](https://reader038.fdocuments.us/reader038/viewer/2022102818/56649cdb5503460f949a6705/html5/thumbnails/5.jpg)
A 8 by 8 chess board. (8 rows, 8 columns)
b4
a8
![Page 6: 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:](https://reader038.fdocuments.us/reader038/viewer/2022102818/56649cdb5503460f949a6705/html5/thumbnails/6.jpg)
Pseudocode 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;
![Page 7: 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:](https://reader038.fdocuments.us/reader038/viewer/2022102818/56649cdb5503460f949a6705/html5/thumbnails/7.jpg)
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
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.
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.
Continued…
![Page 8: 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:](https://reader038.fdocuments.us/reader038/viewer/2022102818/56649cdb5503460f949a6705/html5/thumbnails/8.jpg)
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
Stack s
top
bottom
![Page 9: 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:](https://reader038.fdocuments.us/reader038/viewer/2022102818/56649cdb5503460f949a6705/html5/thumbnails/9.jpg)
Place the first queen, push its position onto s and set filled to 0. int filled =0;
![Page 10: 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:](https://reader038.fdocuments.us/reader038/viewer/2022102818/56649cdb5503460f949a6705/html5/thumbnails/10.jpg)
there are no conflicts, increase filled by 1.
![Page 11: 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:](https://reader038.fdocuments.us/reader038/viewer/2022102818/56649cdb5503460f949a6705/html5/thumbnails/11.jpg)
move to the next row (row 2) and place a queen in the first column. Push its position onto the stack.
![Page 12: 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:](https://reader038.fdocuments.us/reader038/viewer/2022102818/56649cdb5503460f949a6705/html5/thumbnails/12.jpg)
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).
![Page 13: 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:](https://reader038.fdocuments.us/reader038/viewer/2022102818/56649cdb5503460f949a6705/html5/thumbnails/13.jpg)
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
![Page 14: 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:](https://reader038.fdocuments.us/reader038/viewer/2022102818/56649cdb5503460f949a6705/html5/thumbnails/14.jpg)
there are no conflicts, increase filled by 1.
ROW 2, COL 3
![Page 15: 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:](https://reader038.fdocuments.us/reader038/viewer/2022102818/56649cdb5503460f949a6705/html5/thumbnails/15.jpg)
move to the next row and place a queen in the first column. Push its position onto the stack.
ROW 3, COL 1
![Page 16: 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:](https://reader038.fdocuments.us/reader038/viewer/2022102818/56649cdb5503460f949a6705/html5/thumbnails/16.jpg)
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)
![Page 17: 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:](https://reader038.fdocuments.us/reader038/viewer/2022102818/56649cdb5503460f949a6705/html5/thumbnails/17.jpg)
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)
![Page 18: 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:](https://reader038.fdocuments.us/reader038/viewer/2022102818/56649cdb5503460f949a6705/html5/thumbnails/18.jpg)
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)
![Page 19: 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:](https://reader038.fdocuments.us/reader038/viewer/2022102818/56649cdb5503460f949a6705/html5/thumbnails/19.jpg)
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
![Page 20: 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:](https://reader038.fdocuments.us/reader038/viewer/2022102818/56649cdb5503460f949a6705/html5/thumbnails/20.jpg)
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).
![Page 21: 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:](https://reader038.fdocuments.us/reader038/viewer/2022102818/56649cdb5503460f949a6705/html5/thumbnails/21.jpg)
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 2
ROW 3, COL 1
![Page 22: 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:](https://reader038.fdocuments.us/reader038/viewer/2022102818/56649cdb5503460f949a6705/html5/thumbnails/22.jpg)
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 3, COL 2
3
ROW 3, COL 2
3
ROW 4, COL 1
![Page 23: 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:](https://reader038.fdocuments.us/reader038/viewer/2022102818/56649cdb5503460f949a6705/html5/thumbnails/23.jpg)
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 2
3
ROW 4, COL 2
ROW 3, COL 2
3
ROW 4, COL 1
![Page 24: 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:](https://reader038.fdocuments.us/reader038/viewer/2022102818/56649cdb5503460f949a6705/html5/thumbnails/24.jpg)
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 2
3
ROW 4, COL 3
ROW 3, COL 2
3
ROW 4, COL 2
![Page 25: 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:](https://reader038.fdocuments.us/reader038/viewer/2022102818/56649cdb5503460f949a6705/html5/thumbnails/25.jpg)
ROW 3, COL 2
3
ROW 4, COL 4
ROW 3, COL 2
3
ROW 4, COL 3
![Page 26: 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:](https://reader038.fdocuments.us/reader038/viewer/2022102818/56649cdb5503460f949a6705/html5/thumbnails/26.jpg)
ROW 3, COL 3
2
ROW 3, COL 2
3
ROW 4, COL 4
there 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 )
![Page 27: 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:](https://reader038.fdocuments.us/reader038/viewer/2022102818/56649cdb5503460f949a6705/html5/thumbnails/27.jpg)
ROW 3, COL 4
2
ROW 3, COL 3
2
![Page 28: 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:](https://reader038.fdocuments.us/reader038/viewer/2022102818/56649cdb5503460f949a6705/html5/thumbnails/28.jpg)
there 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 4
2ROW 1, COL 1
ROW 2, COL 4
1=filled
![Page 29: 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:](https://reader038.fdocuments.us/reader038/viewer/2022102818/56649cdb5503460f949a6705/html5/thumbnails/29.jpg)
Keep 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 1
ROW 2, COL 4
ROW 1, COL 2
1=filled
0=filled
![Page 30: 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:](https://reader038.fdocuments.us/reader038/viewer/2022102818/56649cdb5503460f949a6705/html5/thumbnails/30.jpg)
ROW 1, COL 2
1=filled
ROW 2, COL 1
ROW 1, COL 2
2=filled
ROW 2, COL 4
Two steps are omitted here.
![Page 31: 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:](https://reader038.fdocuments.us/reader038/viewer/2022102818/56649cdb5503460f949a6705/html5/thumbnails/31.jpg)
ROW 1, COL 2
3=filled
ROW 2, COL 4
ROW 3, COL 1
![Page 32: 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:](https://reader038.fdocuments.us/reader038/viewer/2022102818/56649cdb5503460f949a6705/html5/thumbnails/32.jpg)
ROW 1, COL 2
4=filled
ROW 2, COL 4
ROW 3, COL 1
ROW 4, COL 3
if there are no conflicts increase filled by 1. If filled is now N(N=4), then algorithm is done.
![Page 33: 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:](https://reader038.fdocuments.us/reader038/viewer/2022102818/56649cdb5503460f949a6705/html5/thumbnails/33.jpg)