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

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

Embed Size (px)

Citation preview

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

CPS 100, Fall 2011 10.1

GridGame APT

How would you solve this problem using recursion?

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

CPS 100, Fall 2011 10.2

What we will do today

GridWorld: 1 final example of backtracking Make clear the basic algorithmic approach

NQueens, pathSum, and Boggle’s findWordOnBoard have in common

Subclasses: like interfaces with code in them

A few more hints on Boggle

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

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.

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

CPS 100, Fall 2011 10.4

Basic ideas in backtracking search

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

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 Summary: enumerate choices, try a choice,

undo a choice, this is brute force search: try everything

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

CPS 100, Fall 2011 10.5

Where we are thus far

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

Coming Up: Subclasses: like interfaces but with code

A few more hints on Boggle

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

CPS 100, Fall 2011 10.6

Subclasses: When a Class “extends” Rather than “implements”

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

CPS 100, Fall 2011 10.7

//in Superclass.javaabstract class Superclass{

//just like an interfaceabstract void printStuff();//but has some code toovoid printFoo() { System.out.println(“FOO”); }

}

//in Subclass.javaclass Subclass{

void printStuff() { System.out.println(“STUFF”); }}

//elsewhere//note how similar this is to interfacesSuperClass var = new Subclass();var.printStuff(); //prints STUFFvar.printFoo(); //prints FOO

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

CPS 100, Fall 2011 10.8

Inheritance concepts

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

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

Hard to do OO design, leave for later courses But get an idea now as to power and utility

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

CPS 100, Fall 2011 10.9

Where we are thus far

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

You’ve seen how subclasses work:SuperClass var = new Subclass();var.printStuff(); //calls a subclass methodvar.printFoo(); //calls a superclass method

Coming Up: A few more hints on Boggle

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

CPS 100, Fall 2011 10.10

BinarySearchLexicon

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)

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”)

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

CPS 100, Fall 2011 10.11

Boggle Search for Word

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

How do we know when we're done? Base case of recursive, backtracking call Where we are in the string?

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!

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

CPS 100, Fall 2011 10.12

Base Cases for GoodWordOnBoardFinder

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 square2. You’ve already used this cell3. You’ve found ever letter in the word4. You are off the board Does the order we check these base

cases matter?

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

CPS 100, Fall 2011 10.13

Using Howto Hints

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?

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

CPS 100, Fall 2011 10.14

Where we’ve been 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

You’ve seen how subclasses work:SuperClass var = new Subclass();var.printStuff(); //calls a subclass methodvar.printFoo(); //calls a superclass method 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