CSC 205 Programming II Lecture 18 The Eight Queens Problem.

Slides:



Advertisements
Similar presentations
COSC2007 Data Structures II
Advertisements

8 Queens. Problem: Placing 8 queens on a chessboard such that they don’t attack each other Three different Prolog programs are suggessted as solutions.
Exercise 1 Suppose C is a class that implements interfaces I and J. Which of the following Requires a type cast? C c = ……? I i = …..? J j = …..? c =
Stacks CS 3358 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
Backtracking COP Backtracking  Backtracking is a technique used to solve problems with a large search space, by systematically trying and eliminating.
Backtracking What is backtracking?
© 2006 Pearson Addison-Wesley. All rights reserved6-1 Chapter 6 Recursion as a Problem- Solving Technique.
© 2006 Pearson Addison-Wesley. All rights reserved6-1 More on Recursion.
Chapter 5 Recursion as a Problem-Solving Technique.
Chapter 13 Linked Structures - Stacks. Chapter Scope Object references as links Linked vs. array-based structures Managing linked lists Linked implementation.
CS2110 Recitation 07. Interfaces Iterator and Iterable. Nested, Inner, and static classes We work often with a class C (say) that implements a bag: unordered.
Announcements.
COSC2007 Data Structures II
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 5: Recursion as a Problem-Solving Technique Data Abstraction.
The Java Collections Framework (Part 2) By the end of this lecture you should be able to: Use the HashMap class to store objects in a map; Create objects.
Recursion: Backtracking
CSE 143 Lecture 17 More Recursive Backtracking reading: "Appendix R" on course web site slides created by Marty Stepp and Hélène Martin
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
IMPLEMENTING ARRAYLIST – Part 2 COMP 103. RECAP  Abstract Classes – overview, details in 2 nd year  Implementing the ArrayList: size(), get(), set()
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 5: Recursion as a Problem-Solving Technique Data Abstraction.
CHAPTER 4 RECURSION. BASICALLY, A FUNCTION IS RECURSIVE IF IT INCLUDES A CALL TO ITSELF.
CHAPTER 4 RECURSION. BASICALLY, A METHOD IS RECURSIVE IF IT INCLUDES A CALL TO ITSELF.
Chapter 5 Recursion. Basically, a method is recursive if it includes a call to itself.
Information and Computer Sciences University of Hawaii, Manoa
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
Lab 1 Logbook ADT. OVERVIEW A monthly logbook consists of a set of entries, one for each day of the month.
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"?
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18.
Iterators, Iterator, and Iterable 2015-T2 Lecture 8 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Thomas Kuehne.
CSE 143 Lecture 18 More Recursive Backtracking slides created by Marty Stepp
Iteration Abstraction SWE Software Construction Fall 2009.
CSE 143 Lecture 13 Recursive Backtracking slides created by Ethan Apter
© 2006 Pearson Addison-Wesley. All rights reserved 6-1 Chapter 6 Recursion as a Problem- Solving Technique.
Contest Algorithms January 2016 Pseudo-code for backtracking search, and three examples (all subsets, permutations, and 8- queens). 4. Backtracking 1Contest.
1 Introduction 1. Why Data Structures? 2. What AreData Structure? 3. Phases of Software Development 4. Precondition and Postcondition 5. Examples.
Topic 13 Iterators. 9-2 Motivation We often want to access every item in a data structure or collection in turn We call this traversing or iterating over.
TOWERS OF HANOI. : If n = 1, move disk 1 from pole 'A' to pole 'B'. else: 1.First, move n-1 disks from pole 'A' to pole 'C', using pole 'B' as.
CSE 143 read: 12.5 Lecture 18: recursive backtracking.
1 Iterators & the Collection Classes. 2 » The Collection Framework classes provided in the JAVA API(Application Programmer Interface) contains many type.
Section 2.6 Linked List StringLog ADT Implementation
Intro to Computer Science II
Recursive Exploration II
CSCI 104 Backtracking Search
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
Algorithm Design and Analysis (ADA)
Chapter 4 Linked Structures - Stacks
Exercise: Dice roll sum
adapted from Recursive Backtracking by Mike Scott, UT Austin
Ch. 6 Recursion as a Problem Solving Technique
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
Algorithms: Design and Analysis
CSE 143 Lecture 18 More Recursive Backtracking
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"?
Objects with ArrayLists as Attributes
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
CSC 205 – Java Programming II
Presentation transcript:

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 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 1-east3-west 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 Position class 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 int column () { return column ; } }

Define the Problem – the QueensIterator class 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 next method // 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 EightQueens class 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

The Eight Queens Problem Q