Boggle Intro to recursive backtracking

advertisement

Boggle

Intro to recursive backtracking

Intro to Boggle

Intro to recursive backtracking

Practice (with an APT)

Today

How to play

How to play

How to find words?

You have a dictionary

You want to find all words on board

How?

Write out step-bystep algorithm

Brute force

Find everything!

N

N-Z

N-Z-I

N-Z-V

N-Z-E

N-Z-A

N-E

N-A

Is there a better way?

Find everything!

N

N-Z

N-Z-I

N-Z-V

N-Z-E

N-Z-A

N-E

N-A

Is there a better way?

Find everything!

N

N-Z

N-Z-I

N-Z-V

N-Z-E

N-Z-A

N-E

N-A

Prefixes

Look at word prefixes

• If prefix doesn’t make a word, STOP

We should store our words in a data structure to help us

Tree-like structure

Node

• the character to reach node

• if the node forms a word

• denoted in red

• map of children

<Character, Node>

• parent node

Tries

Let’s trie it!

• “Peter piper picked a peck of pickled peppers”

Recursion void solve (ProblemClass instance){ base cases recursion reassemble problem

}

Bracktracking

Classic problem – N Queens

On an NxN board, how do you place N queens such that no queens can attack each other

Backtracking

• place a queen on board

• if ok, keep going

• else, remove queen

4/11/2020 13

Recursive backtracking void solve (ProblemClass instance){ base cases save current state recursion if current state DIDN’T work remove current state reassemble problem

}

4/11/2020

Grid Game

15

Download