8-Queens Puzzle.

Slides:



Advertisements
Similar presentations
COSC2007 Data Structures II
Advertisements

Polynomial Time Algorithms for the N-Queen Problem Rok sosic and Jun Gu.
Solving N+k Queens Using Dancing Links Matthew A. Wolff Morehead State University May 19, 2006.
PROLOG 8 QUEENS PROBLEM.
BackTracking Algorithms
Swap I class Swap { public static void swap(int i, int j) {
Statement of the Problem Problem - how to place eight queens on a chessboard so that no two queens can attack each other:
1 Applications of Recursion (Walls & Mirrors - Chapter 5)
Lecture 17: Spanning Trees Minimum Spanning Trees.
COSC2007 Data Structures II
CSE 143 Lecture 17 More Recursive Backtracking reading: "Appendix R" on course web site slides created by Marty Stepp and Hélène Martin
Back Tracking Project Due August 11, 1999 N-queens: –A classic puzzle for chess buffs is the N- Queens problem. Simply stated: is it possible to place.
NxN Queens Problem Q- -- -Q Q- -- -Q Q- -- QQ -- Q- Q- Q- -Q QQ -- -Q -- -Q Q- -Q -Q Q- Q- -Q Q- -- Q- -- QQ Q- -Q -Q -Q -- QQ -- -Q START.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
CSE 143 Lecture 17 More Recursive Backtracking reading: "Appendix R" on course web site slides created by Marty Stepp and Hélène Martin
Two Dimensional Arrays
CSC 205 Programming II Lecture 18 The Eight Queens Problem.
Announcements This Wednesday, Class and Labs are cancelled! The last lab is due this Wednesday … how many people are planning on doing it? Finally posted.
Data Structures Using C++ 2E1 Recursion and Backtracking: DFS Depth first search (a way to traverse a tree or graph) Backtracking can be regarded as a.
Sudoku Jordi Cortadella Department of Computer Science.
Lec 20 More Arrays--Algorithms. Agenda Array algorithms (section 7.5 in the book) – An algorithm is a well-defined specification for solving a problem.
Two Dimensional Arrays. Two-dimensional Arrays Declaration: int matrix[4][11]; 4 x 11 rows columns
TIC TAC TOE. import java.util.Scanner; import java.util.Random; public class PlayTTT{ public static void main(String[]args){ Scanner reader = new Scanner(System.in);
CPS Backtracking, Search, Heuristics l Many problems require an approach similar to solving a maze  Certain mazes can be solved using the “right-hand”
1 Data Structures CSCI 132, Spring 2014 Lecture 17 Backtracking.
The "8 Queens" problem Consider the problem of trying to place 8 queens on a chess board such that no queen can attack another queen. What are the "choices"?
CS 100Lecture 191 CS100J Lecture 19 n Previous Lecture –Two dimensional arrays. –Reasonable size problem (a past assignment). –Stepwise refinement. –Use.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
CSE 143 Lecture 18 More Recursive Backtracking slides created by Marty Stepp
Backtracking & Brute Force Optimization Intro2CS – weeks
CSE 143 Lecture 13 Recursive Backtracking slides created by Ethan Apter
Contest Algorithms January 2016 Pseudo-code for backtracking search, and three examples (all subsets, permutations, and 8- queens). 4. Backtracking 1Contest.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
Recursion – some examples. Sum of Squares Write Vertical.
CSE 143 read: 12.5 Lecture 18: recursive backtracking.
1 Tirgul 11: Recursion & Backtracking. 2 Elements of a recursive solution (Reminder) A base case that is so simple we need no computation to solve it.
A 2-D Array is a structure that storage space both vertically and horizontally. Thus, the array has both rows and columns. 2-D Arrays are used to create.
Various Problem Solving Approaches. Problem solving by analogy Very often problems can be solved by looking at similar problems. For example, consider.
Backtracking, Search, Heuristics
Recursion -- Introduction
CS100J Lecture 19 Previous Lecture This Lecture
Intro to Computer Science II
CSCI 104 Backtracking Search
Data Structures and Algorithms
Sit-In Lab 1 Ob-CHESS-ion
The "8 Queens" problem Consider the problem of trying to place 8 queens on a chess board such that no queen can attack another queen. What are the "choices"?
CSE 143 Lecture 19 More Recursive Backtracking
Java Software Structures: John Lewis & Joseph Chase
Chapter 12 Recursion (methods calling themselves)
null, true, and false are also reserved.
Analysis and design of algorithm
Jordi Cortadella Department of Computer Science
Lecture 13: Tree Traversals Algorithms on Trees.
Two-Dimensional Arrays
class PrintOnetoTen { public static void main(String args[]) {
Exercise: Dice roll sum Write a method diceSum similar to diceRoll, but it also accepts a desired sum and prints only arrangements that add up to.
Exercise: Dice roll sum
CSE 143 Lecture 18 More Recursive Backtracking
CSC 172 DATA STRUCTURES.
Building Java Programs
Rules to play chess. Chess is a played with 16 pieces: 8 pawns, 2 towers, 2 knights, 2 bishops, 1 queen and 1 king. Movements: Pawns: They only can move.
CSE 143 Lecture 18 More Recursive Backtracking
Exercise: Dice roll sum
The "8 Queens" problem Consider the problem of trying to place 8 queens on a chess board such that no queen can attack another queen. What are the "choices"?
22C:21 Problem 2.3 Solution Outline.
Backtracking, Search, Heuristics
Principles of Object Oriented Programming
Data Structures & Programming
Data Structures & Programming
Presentation transcript:

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 The queen - moves horizontally, vertically, or diagonally. Can attack any piece on its way.

Basic Rules The queen - moves horizontally, vertically, or diagonally. Can attack any piece on its way.

Basic Rules Two queens threaten each other if they are on the same vertical, horizontal, or diagonal line.

Basic Rules Two queens threaten each other if they are on the same vertical, horizontal, or diagonal line.

8-Queens puzzle Place 8 queens on the board such that no two queens are threatening each other.

8-Queens puzzle Place 8 queens on the board such that no two queens are threatening each other.

Recursive (non-OOP) solution

Place a queen at a non-threatened cell.

Place a queen at a non-threatened cell.

Place a queen at a non-threatened cell.

Place a queen at a non-threatened cell.

Place a queen at a non-threatened cell.

Place a queen at a non-threatened cell.

Place a queen at a non-threatened cell.

Place a queen at a non-threatened cell.

Backtrack.

Backtrack.

Place a queen at a non-threatened cell.

Place a queen at a non-threatened cell.

Place a queen at a non-threatened cell. Backtrack…

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;

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;

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

OOP solution

Main ideas Each queen is an autonomous agent! Each queen has its own fixed column. 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.)

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

A queen places itself at a safe position in its column.

A queen places itself at a safe position in its column.

A queen places itself at a safe position in its column.

A queen places itself at a safe position in its column.

A queen places itself at a safe position in its column.

No safe position for queen 6 at column 6. Asks neighbor (queen 5) to change position.

No safe position for queen 6 at column 6. Asks neighbor (queen 5) to change position.

Queen 5 is at the last row. Asks neighbor (queen 4) to change position.

A queen places itself at a safe position in its column.

A queen places itself at a safe position in its column.

A queen places itself at a safe position in its column.

A queen places itself at a safe position in its column.

No safe position for queen 8 at column 8. Proceed the search in a similar way…

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();

public boolean findSolution() { while (this.neighbor != null && this.neighbor.canAttack(this.row, this.column)) boolean advanced = this.advance(); if (!advanced) return false; return true; }

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; }

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 }