More Recursion
Binary Strings "" 3 steps to go "0" "1" 2 steps to go "00" "01" "10" "11" 1 step to go "000" "001" "010" "011" "100" "101" "110" "111" done
Binary Strings
Towers Of Hanoi 3 Pegs & Stack of disks Get from one peg to another Can only move one disk at a time Big disks can not be on top of small disks
Decomposition To move 3 disks from A to C Move top 2 to B Move last to C Move 2 from B to C
Towers Of Hanoi 3 disks = 7 moves N disks = 2n – 1 moves
N-Queens Eight Queens Each must be on own row/col/diagonal
Representation Single array : what col is queen in?
Code Do not need to copy – moving queen overwrites old move
Flood Fill Flood Fill Color starting square Try to color square above Try to color square below Try to color square to left Try to color square to right
Shared Data Flood fill works on one copy of data Array passed as memory addresses Every recursive call uses same array
Path Finding Find a path… If goal, done = return 0 If bad square return Else Mark square Ask left how far Ask right how far Ask up how far Ask down how far Return 1 + shortest of neighbors
Path Finding
Path Finding Want to explore different paths Each path needs own array Need to copy current maze before marking move