Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPS 100, Fall 2011 10.1 GridGame APT How would you solve this problem using recursion?

Similar presentations


Presentation on theme: "CPS 100, Fall 2011 10.1 GridGame APT How would you solve this problem using recursion?"— Presentation transcript:

1 CPS 100, Fall 2011 10.1 GridGame APT How would you solve this problem using recursion?

2 CPS 100, Fall 2011 10.2 What we will do today l GridWorld: 1 final example of backtracking  Make clear the basic algorithmic approach NQueens, pathSum, and Boggle’s findWordOnBoard have in common l Subclasses: like interfaces with code in them l A few more hints on Boggle

3 CPS 100, Fall 2011 10.3 GridGame APT Key insight: if my opponent has 0 winning moves after I make a move, then that move guarantees I win.

4 CPS 100, Fall 2011 10.4 Basic ideas in backtracking search l Enumerate all possible choices/moves  We try these choices in order, committing to a choice  If the choice doesn't pan out we must undo the choice Backtracking step, choices must be undoable l Inherently recursive, when to stop searching?  When all columns tried in N queens  When a player has won or lost the gridgame  When the sum has been found, or when we know the sum cannot be found because we’ve gone over  In general: when we know if our choices have led to success or failure l Summary: enumerate choices, try a choice, undo a choice, this is brute force search: try everything

5 CPS 100, Fall 2011 10.5 Where we are thus far l You have seen the general idea of backtracking:  Step 1: Make a choice  Step 2: Check to see if the choice leads to success  Step 3: If not, undo the choice and make the next one l Coming Up: Subclasses: like interfaces but with code l A few more hints on Boggle

6 CPS 100, Fall 2011 10.6 Subclasses: When a Class “extends” Rather than “implements”

7 CPS 100, Fall 2011 10.7 //in Superclass.java abstract class Superclass { //just like an interface abstract void printStuff(); //but has some code too void printFoo() { System.out.println(“FOO”); } } //in Subclass.java class Subclass { void printStuff() { System.out.println(“STUFF”); } } //elsewhere //note how similar this is to interfaces SuperClass var = new Subclass(); var.printStuff(); //prints STUFF var.printFoo(); //prints FOO

8 CPS 100, Fall 2011 10.8 Inheritance concepts l Parent/super class can be extended by subclassing  Possible to use methods from parent class, subs have them!  Possible to override parent methods, change behavior  Possible to do both! Call super.doThis(); l Often you don't have access to parent.java file  Still can subclass, use methods, extend/override them  Do NOT have access to private data fields  DO have access to protected data fields l Hard to do OO design, leave for later courses  But get an idea now as to power and utility

9 CPS 100, Fall 2011 10.9 Where we are thus far l You have seen the general idea of backtracking:  Step 1: Make a choice  Step 2: Check to see if the choice leads to success  Step 3: If not, undo the choice and make the next one l You’ve seen how subclasses work: SuperClass var = new Subclass(); var.printStuff(); //calls a subclass method var.printFoo(); //calls a superclass method l Coming Up: A few more hints on Boggle

10 CPS 100, Fall 2011 10.10 BinarySearchLexicon l You do not have to implement binary search (it’s in Collections.binarySearch) BUT you do have to sort the list before you use binary search (the provided code does this sort for you…just be aware in general) l Three results from Lexicon (see SimpleLexicon for an example of how to return these)  NOT A WORD (e.g. “zz”)  A WORD (e.g. “do”)  Not a word, but the PREFIX of a word (e.g. “dy”)

11 CPS 100, Fall 2011 10.11 Boggle Search for Word l Starting at board location (row,col): find a string S  We want to keep track of where we are in the string  Also track what board locations used for S search l How do we know when we're done?  Base case of recursive, backtracking call  Where we are in the string? l How do we keep track of used locations?  Store in array list: tentatively use current one, recurse  If we don’t succeed, take off the last one stored!

12 CPS 100, Fall 2011 10.12 Base Cases for GoodWordOnBoardFinder l In to find a particular word on the board, I can think of 4 different kinds of base cases. 1. Word is not at this square 2. You’ve already used this cell 3. You’ve found ever letter in the word 4. You are off the board l Does the order we check these base cases matter?

13 CPS 100, Fall 2011 10.13 Using Howto Hints l Helper method in GoodWordOnBoardFinder  Needed: board, row, col, word, word-index, list  Search for "skunk" at (0,0) at (0,1) at …  Call help(board,row,col,"skunk",0,list)  If we find the 's', where do we look next and for what?  If we don't find the 's' what does that mean?  If (row,col) isn't legal position then …  If we've found the last 'k' what does that mean?

14 CPS 100, Fall 2011 10.14 Where we’ve been l You have seen the general idea of backtracking:  Step 1: Make a choice  Step 2: Check to see if the choice leads to success  Step 3: If not, undo the choice and make the next one l You’ve seen how subclasses work: SuperClass var = new Subclass(); var.printStuff(); //calls a subclass method var.printFoo(); //calls a superclass method l A few more hints on Boggle  You don’t have to write your own search for BinarySearchLexicon  Be careful with your base cases in GoodWordOnBoardFinder


Download ppt "CPS 100, Fall 2011 10.1 GridGame APT How would you solve this problem using recursion?"

Similar presentations


Ads by Google