Skip to content
BytePatterns

Disc Tower Moves

MediumRecursion#recursion#divide-and-conquer~20m

Problem

Three pegs are named A, B and C. Peg A holds n discs of different sizes, largest at the bottom. Move the whole stack to peg C, one disc at a time, never placing a larger disc on a smaller one. Return the moves in order, each written as the peg moved from and the peg moved to.

Examples

Input:  n = 1, src = "A", dst = "C", spare = "B"
Output: [("A", "C")]
Why:    a single disc goes straight across
Input:  n = 2, src = "A", dst = "C", spare = "B"
Output: [("A", "B"), ("A", "C"), ("B", "C")]
Why:    park the small disc on the spare peg, move the big one, put it back
Input:  n = 0, src = "A", dst = "C", spare = "B"
Output: []
Why:    edge case, an empty stack needs no moves

Hints

0 / 3

Stuck on the idea rather than the code? The Call Stack covers it.