160 likes | 287 Views
CSC 205 Programming II. Lecture 18 The Eight Queens Problem. Recap: Backtracking. The strategy guessing at a solution and backing up when an impasse is reached The solution template A general-purpose BackTrack class Application specific classes
E N D
CSC 205Programming II Lecture 18 The Eight Queens Problem
Recap: Backtracking • The strategy • guessing at a solution and backing up when an impasse is reached • The solution template • A general-purpose BackTrack class • Application specific classes • A class implementing the Application interface • A Position class • A class implementing the Java Iterator interface
The tryToSolve Method boolean success = false; Iterator itr = app.iterator (pos); while (!success && itr.hasNext()) { pos = (Position)itr.next(); if (app.valid (pos)) { app.record (pos); if (app.done (pos)) success = true; else { success = tryToSolve (pos); if (!success) app.undo (pos); } // not done } // a valid position } // while return success; 0-north 3-west 1-east 2-south
The Eight Queens Problem • A queen can attack pieces in her row, in her column, or in either of her diagonals • The goal is to put eight queens on a board and none of them is under attack • An efficient solution is needed • There are 4,426,165,368 ways to arrange 8 queens on a chessboard of 64 squares • The number is reduced to 40,320 after taking the fact that no two queens can be in the same row or column into account
Key Elements • The start position • The square in which the first queen is placed • The finish position • The first position on the last column which is not under attack • The way to iterate • Place the next queen in the column right to the current one, starting from the first row; check against the existing queen(s) • Put the queen in the first row which is not under attack • Backtrack if all eight rows have been tried in vain
Scenario I • Let’s use a smaller board (4X4) and place the first queen in the first square on the board (in the upper-left corner) Q
Scenario II • As a second example, let’s place the first queen in the second square in the first column Q
Define the Problem – the Positionclass public class Position { protected int row, column; public Position () { row = 0; column = 0; } // constructor public Position (int row, int column) { this.row = row; this.column = 0; } // constructor public int row () { return row; } public intcolumn () { returncolumn ; } }
Define the Problem – the QueensIteratorclass private class QueensIterator implements Iterator { int row, column; int count = 0; public QueensIterator (Position pos) { row = pos.row(); column = pos.column(); } // constructor public boolean hasNext() { return count < 8; } // method hasNext
Define the Problem – the nextmethod // Precondition: 0 <= count <= 7. // Postcondition: the choice for the next Position has been //returned. public Object next() { Position nextPosition = new Position(); //add your code here: set row to the right value return nextPosition; } // method next public void remove() { throw new UnsupportedOperationException(); } // method next } // class QueensIterator
Define the Problem – the EightQueensclass • This class implements the Application interface (see the next two slides for details) • Instance variables • Methods to be implemented • The valid method • The done method • The undo method • The record method • The toString method
The Application Interface import java.util.*; public interface Application { // Postcondition: true has been returned if pos could be on a //path to a goal position. Otherwise, false has been returned. public boolean valid (Position pos); // Postcondition: the position specified by pos has been //marked as being on a path to a goal position. public void record (Position pos); // Postcondition: true has been returned if pos is a goal //position. Otherwise, false has been returned. public boolean done (Position pos);
The Application Interface // Postcondition: the position specified by pos has been //marked as not being on a path to a goal position. public void undo (Position pos); // Postcondition: a string representation of this Application has //been returned. public String toString(); // Postcondition: an iterator over the positions directly //accessible from pos has been returned. public Iterator iterator (Position pos); } // interface Application
In The Heart of the Solution– When Should valid Return true? • false should be returned when • Out of the valid range • The attempted queen is under attack • In the same row as an existing queen • In the same column as an existing queen • In the same descending diagonal as an existing queen • In the same ascending diagonal as an existing queen • Otherwise, true should be returned
Another Example Key West,FL:Miami,FL-70 Pensacola,FL:Tallahassee,FL-120 St Petersburg,FL:Tampa,FL-20;Naples,FL-100 Miami,FL:Fort Lauderdale,FL-15;Key West,FL-70 Tallahassee,FL:Pensacola,FL-120;Lake City,FL-70 Jacksonville,FL:Lake City,FL-50;Daytona Beach,FL-90 Naples,FL:Fort Lauderdale,FL-90;St Petersburg,FL-100 Orlando,FL:Daytona Beach,FL-40;Lake City,FL-120;Tampa,FL-60 Tampa,FL:Orlando,FL-60;St Petersburg,FL-20;Lake City,FL-125 Fort Pierce,FL:Daytona Beach,FL-110;Fort Lauderdale,FL-50 Fort Lauderdale,FL:Fort Pierce,FL-90;Naples,FL-90;Miami,FL-15 Daytona Beach,FL:Orlando,FL-40;Jacksonville,FL-90;Fort Pierce,FL-110 Lake City,FL:Tallahassee,FL-70;Orlando,FL-120;Jacksonville,FL-50;Tampa,FL-125