Boggle Intro to recursive backtracking Today Intro to
Boggle Intro to recursive backtracking
Today • Intro to Boggle • Intro to recursive backtracking • Practice (with an APT)
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-by-step algorithm
Brute force • Find everything! • 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-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-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
Tries • 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
Let’s trie it! • “Peter piper picked a peck of pickled peppers”
Recursion void solve(Problem. Class instance){ base cases recursion reassemble problem }
Bracktracking • Classic problem – N Queens • On an Nx. N 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 9/25/2020 13
Recursive backtracking void solve(Problem. Class instance){ base cases save current state recursion if current state DIDN’T work remove current state reassemble problem }
Grid Game 9/25/2020 15
- Slides: 15