8-queens puzzle
DESCRIPTION
8-Queens Puzzle. Basic Rules. The board: a matrix of size N X N. In standard chess: N = 8. Basic Rules. The queen - moves horizontally, vertically, or diagonally. Basic Rules. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: 8-Queens Puzzle](https://reader035.vdocuments.net/reader035/viewer/2022062809/5681584e550346895dc5a85b/html5/thumbnails/1.jpg)
8-Queens Puzzle
![Page 2: 8-Queens Puzzle](https://reader035.vdocuments.net/reader035/viewer/2022062809/5681584e550346895dc5a85b/html5/thumbnails/2.jpg)
Basic RulesThe board: a matrix of size N X N.
In standard chess: N = 8.
![Page 3: 8-Queens Puzzle](https://reader035.vdocuments.net/reader035/viewer/2022062809/5681584e550346895dc5a85b/html5/thumbnails/3.jpg)
Basic Rules The queen - moves horizontally, vertically, or
diagonally.
![Page 4: 8-Queens Puzzle](https://reader035.vdocuments.net/reader035/viewer/2022062809/5681584e550346895dc5a85b/html5/thumbnails/4.jpg)
The queen - moves horizontally, vertically, or diagonally.
Can attack any piece on its way.
Basic Rules
![Page 5: 8-Queens Puzzle](https://reader035.vdocuments.net/reader035/viewer/2022062809/5681584e550346895dc5a85b/html5/thumbnails/5.jpg)
The queen - moves horizontally, vertically, or diagonally.
Can attack any piece on its way.
Basic Rules
![Page 6: 8-Queens Puzzle](https://reader035.vdocuments.net/reader035/viewer/2022062809/5681584e550346895dc5a85b/html5/thumbnails/6.jpg)
Basic Rules
Two queens threaten each other if they are on the same vertical, horizontal, or diagonal line.
![Page 7: 8-Queens Puzzle](https://reader035.vdocuments.net/reader035/viewer/2022062809/5681584e550346895dc5a85b/html5/thumbnails/7.jpg)
Basic Rules
Two queens threaten each other if they are on the same vertical, horizontal, or diagonal line.
![Page 8: 8-Queens Puzzle](https://reader035.vdocuments.net/reader035/viewer/2022062809/5681584e550346895dc5a85b/html5/thumbnails/8.jpg)
8-Queens puzzlePlace 8 queens on the board such that
no two queens are threatening each other.
![Page 9: 8-Queens Puzzle](https://reader035.vdocuments.net/reader035/viewer/2022062809/5681584e550346895dc5a85b/html5/thumbnails/9.jpg)
8-Queens puzzlePlace 8 queens on the board such that
no two queens are threatening each other.
![Page 10: 8-Queens Puzzle](https://reader035.vdocuments.net/reader035/viewer/2022062809/5681584e550346895dc5a85b/html5/thumbnails/10.jpg)
Recursive (non-OOP) solution
![Page 11: 8-Queens Puzzle](https://reader035.vdocuments.net/reader035/viewer/2022062809/5681584e550346895dc5a85b/html5/thumbnails/11.jpg)
Place a queen at a non-threatened cell.
![Page 12: 8-Queens Puzzle](https://reader035.vdocuments.net/reader035/viewer/2022062809/5681584e550346895dc5a85b/html5/thumbnails/12.jpg)
Place a queen at a non-threatened cell.
![Page 13: 8-Queens Puzzle](https://reader035.vdocuments.net/reader035/viewer/2022062809/5681584e550346895dc5a85b/html5/thumbnails/13.jpg)
Place a queen at a non-threatened cell.
![Page 14: 8-Queens Puzzle](https://reader035.vdocuments.net/reader035/viewer/2022062809/5681584e550346895dc5a85b/html5/thumbnails/14.jpg)
Place a queen at a non-threatened cell.
![Page 15: 8-Queens Puzzle](https://reader035.vdocuments.net/reader035/viewer/2022062809/5681584e550346895dc5a85b/html5/thumbnails/15.jpg)
Place a queen at a non-threatened cell.
![Page 16: 8-Queens Puzzle](https://reader035.vdocuments.net/reader035/viewer/2022062809/5681584e550346895dc5a85b/html5/thumbnails/16.jpg)
Place a queen at a non-threatened cell.
![Page 17: 8-Queens Puzzle](https://reader035.vdocuments.net/reader035/viewer/2022062809/5681584e550346895dc5a85b/html5/thumbnails/17.jpg)
Place a queen at a non-threatened cell.
![Page 18: 8-Queens Puzzle](https://reader035.vdocuments.net/reader035/viewer/2022062809/5681584e550346895dc5a85b/html5/thumbnails/18.jpg)
Place a queen at a non-threatened cell.
![Page 19: 8-Queens Puzzle](https://reader035.vdocuments.net/reader035/viewer/2022062809/5681584e550346895dc5a85b/html5/thumbnails/19.jpg)
Backtrack.
![Page 20: 8-Queens Puzzle](https://reader035.vdocuments.net/reader035/viewer/2022062809/5681584e550346895dc5a85b/html5/thumbnails/20.jpg)
Backtrack.
![Page 21: 8-Queens Puzzle](https://reader035.vdocuments.net/reader035/viewer/2022062809/5681584e550346895dc5a85b/html5/thumbnails/21.jpg)
Place a queen at a non-threatened cell.
![Page 22: 8-Queens Puzzle](https://reader035.vdocuments.net/reader035/viewer/2022062809/5681584e550346895dc5a85b/html5/thumbnails/22.jpg)
Place a queen at a non-threatened cell.
![Page 23: 8-Queens Puzzle](https://reader035.vdocuments.net/reader035/viewer/2022062809/5681584e550346895dc5a85b/html5/thumbnails/23.jpg)
Place a queen at a non-threatened cell.
Backtrack…
![Page 24: 8-Queens Puzzle](https://reader035.vdocuments.net/reader035/viewer/2022062809/5681584e550346895dc5a85b/html5/thumbnails/24.jpg)
public static void main(String args[]){ final int N = 8; int [][] board = new int[N][N]; init(board); solve(board,N); }public static boolean solve(int [][] board, int cnt){ if (cnt == 0){ print(board); return true; } boolean solved = false; if (cnt > 0 && !solved){ for (int i = 0; i < board.length && !solved; i++) for (int j =0; j < board[i].length && !solved; j++) if (board[i][j] == FREE){ //FREE – a constant, equals 0 int [][] newBoard = cloneBoard(board); newBoard[i][j] = QUEEN; // QUEEN - a constant, equals 1 threaten(newBoard,i,j); solved = solve(newBoard, cnt - 1); } } return solved;}
![Page 25: 8-Queens Puzzle](https://reader035.vdocuments.net/reader035/viewer/2022062809/5681584e550346895dc5a85b/html5/thumbnails/25.jpg)
public static void threaten(int [][] board, int i, int j){for (int x = 0; x < board[i].length; x++){ if (board[i][x] == FREE) board[i][x] = THREAT; // const. eq. 2 }for (int y = 0; y < board.length; y++ ){ if (board[y][j] == FREE) board[y][j] = THREAT; }int ltx,lty, rtx,rty, lbx,lby, rbx, rby;ltx = rtx = lbx = rbx = i;lty = rty = lby = rby = j;for (int z = 0; z < board.length; z++){ if (board[ltx][lty] == FREE) board[ltx][lty] = THREAT; if (board[rtx][rty] == FREE) board[rtx][rty] = THREAT; if (board[lbx][lby] == FREE) board[lbx][lby] = THREAT; if (board[rbx][rby] == FREE) board[rbx][rby] = THREAT;
![Page 26: 8-Queens Puzzle](https://reader035.vdocuments.net/reader035/viewer/2022062809/5681584e550346895dc5a85b/html5/thumbnails/26.jpg)
public static void threaten(int [][] board, int i, int j){for (int x = 0; x < board[i].length; x++){ if (board[i][x] == FREE) board[i][x] = THREAT; // const. eq. 2 }for (int y = 0; y < board.length; y++ ){ if (board[y][j] == FREE) board[y][j] = THREAT; }int ltx,lty, rtx,rty, lbx,lby, rbx, rby;ltx = rtx = lbx = rbx = i;lty = rty = lby = rby = j;for (int z = 0; z < board.length; z++){ if (board[ltx][lty] == FREE) board[ltx][lty] = THREAT; if (board[rtx][rty] == FREE) board[rtx][rty] = THREAT; if (board[lbx][lby] == FREE) board[lbx][lby] = THREAT; if (board[rbx][rby] == FREE) board[rbx][rby] = THREAT;
if (ltx >0 && lty >0){ ltx--; lty--; } if (rbx < board.length - 1 && rby < board.length - 1 ){ rbx++; rby++; } if (rtx < board.length -1 && rty > 0){ rtx++; rty--; } if (lbx > 0 && lby < board.length - 1){ lbx--; lby++; }
} //end of for
} // end of function threaten
![Page 27: 8-Queens Puzzle](https://reader035.vdocuments.net/reader035/viewer/2022062809/5681584e550346895dc5a85b/html5/thumbnails/27.jpg)
OOP solution
![Page 28: 8-Queens Puzzle](https://reader035.vdocuments.net/reader035/viewer/2022062809/5681584e550346895dc5a85b/html5/thumbnails/28.jpg)
• Each queen is an autonomous agent!
Main ideas
• Queens are added to the board from left to right.
• A queen tries to find a safe position in its column.
• If no safe position is found,
then the queen asks its neighbors to advance to the next legal position.
(In which no two neighbors threaten each other.)
• Each queen has its own fixed column.
![Page 29: 8-Queens Puzzle](https://reader035.vdocuments.net/reader035/viewer/2022062809/5681584e550346895dc5a85b/html5/thumbnails/29.jpg)
Queen class diagram
Queen
- row // current row number (changes)- column // column number (fixed)- neighbor // neighbor to left (fixed)
+ findSolution // find acceptable solution for self and neighbors
+ advance // advance row and find next acceptable solution
+ canAttack // see whether a position can be attacked by self or neighbors
![Page 30: 8-Queens Puzzle](https://reader035.vdocuments.net/reader035/viewer/2022062809/5681584e550346895dc5a85b/html5/thumbnails/30.jpg)
A queen places itself at a safe position in its column.
![Page 31: 8-Queens Puzzle](https://reader035.vdocuments.net/reader035/viewer/2022062809/5681584e550346895dc5a85b/html5/thumbnails/31.jpg)
A queen places itself at a safe position in its column.
![Page 32: 8-Queens Puzzle](https://reader035.vdocuments.net/reader035/viewer/2022062809/5681584e550346895dc5a85b/html5/thumbnails/32.jpg)
A queen places itself at a safe position in its column.
![Page 33: 8-Queens Puzzle](https://reader035.vdocuments.net/reader035/viewer/2022062809/5681584e550346895dc5a85b/html5/thumbnails/33.jpg)
A queen places itself at a safe position in its column.
![Page 34: 8-Queens Puzzle](https://reader035.vdocuments.net/reader035/viewer/2022062809/5681584e550346895dc5a85b/html5/thumbnails/34.jpg)
A queen places itself at a safe position in its column.
![Page 35: 8-Queens Puzzle](https://reader035.vdocuments.net/reader035/viewer/2022062809/5681584e550346895dc5a85b/html5/thumbnails/35.jpg)
No safe position for queen 6 at column 6.
Asks neighbor (queen 5) to change position.
![Page 36: 8-Queens Puzzle](https://reader035.vdocuments.net/reader035/viewer/2022062809/5681584e550346895dc5a85b/html5/thumbnails/36.jpg)
No safe position for queen 6 at column 6.
Asks neighbor (queen 5) to change position.
![Page 37: 8-Queens Puzzle](https://reader035.vdocuments.net/reader035/viewer/2022062809/5681584e550346895dc5a85b/html5/thumbnails/37.jpg)
Queen 5 is at the last row.
Asks neighbor (queen 4) to change position.
![Page 38: 8-Queens Puzzle](https://reader035.vdocuments.net/reader035/viewer/2022062809/5681584e550346895dc5a85b/html5/thumbnails/38.jpg)
![Page 39: 8-Queens Puzzle](https://reader035.vdocuments.net/reader035/viewer/2022062809/5681584e550346895dc5a85b/html5/thumbnails/39.jpg)
A queen places itself at a safe position in its column.
![Page 40: 8-Queens Puzzle](https://reader035.vdocuments.net/reader035/viewer/2022062809/5681584e550346895dc5a85b/html5/thumbnails/40.jpg)
A queen places itself at a safe position in its column.
![Page 41: 8-Queens Puzzle](https://reader035.vdocuments.net/reader035/viewer/2022062809/5681584e550346895dc5a85b/html5/thumbnails/41.jpg)
A queen places itself at a safe position in its column.
![Page 42: 8-Queens Puzzle](https://reader035.vdocuments.net/reader035/viewer/2022062809/5681584e550346895dc5a85b/html5/thumbnails/42.jpg)
A queen places itself at a safe position in its column.
![Page 43: 8-Queens Puzzle](https://reader035.vdocuments.net/reader035/viewer/2022062809/5681584e550346895dc5a85b/html5/thumbnails/43.jpg)
No safe position for queen 8 at column 8.
Proceed the search in a similar way…
![Page 44: 8-Queens Puzzle](https://reader035.vdocuments.net/reader035/viewer/2022062809/5681584e550346895dc5a85b/html5/thumbnails/44.jpg)
public class Queen{
…
public Queen (int column, Queen neighbor) {
this.row = 1;
this.column = column;
this.neighbor = neighbor;
}
…..
public static void main(String args[]){
Queen lastQueen = null; for (int i = 1; i <= N; i++) { \\ N equals 8
lastQueen = new Queen(i, lastQueen);lastQueen.findSolution();
}
![Page 45: 8-Queens Puzzle](https://reader035.vdocuments.net/reader035/viewer/2022062809/5681584e550346895dc5a85b/html5/thumbnails/45.jpg)
public boolean findSolution() {
while (this.neighbor != null && this.neighbor.canAttack(this.row, this.column))
boolean advanced = this.advance();
if (!advanced) return false;
return true;
}
![Page 46: 8-Queens Puzzle](https://reader035.vdocuments.net/reader035/viewer/2022062809/5681584e550346895dc5a85b/html5/thumbnails/46.jpg)
private boolean canAttack(int testRow, int testColumn) {
int columnDifference = testColumn – this.column;
if ((this.row == testRow) ||
(this.row + columnDifference == testRow) ||
(this.row - columnDifference == testRow))
return true;
if (this.neighbor != null)
return neighbor.canAttack(testRow, testColumn);
return false;
}
![Page 47: 8-Queens Puzzle](https://reader035.vdocuments.net/reader035/viewer/2022062809/5681584e550346895dc5a85b/html5/thumbnails/47.jpg)
public boolean advance() {
if (this.row < N) { \\ N equals 8
this.row++;
return this.findSolution();
{
if (this.neighbor != null) {
boolean advanced = this.neighbor.advance());
if (!advanced)
return false;
row = 1;
return findSolution();
{
else
return false;
}