snippetMinor
How to solve Tower of Hanoi iteratively?
Viewed 0 times
hanoisolvehowiterativelytower
Problem
It may seem obvious to many but i am having a hard time figuring out the iterative solution to the Tower of Hanoi problem. There are some solutions on the Internet but without explanations. So can anybody give a sound explanation so that it becomes more intuitive and easy to reason.
Solution
You can transform the recursive solution to an iterative solution. To do this, create a stack that will contain items consisting of quadruples ("from", "to", "via", "num_disks"). For every function "call" in your recursive algorithm, push the parameters to a stack in the iterative algorithm. For every function "return" in your recursive algorithm, pop the parameters from the stack in the iterative algorithm.
Wikipedia has the complete iterative solution.
Wikipedia has the complete iterative solution.
Context
StackExchange Computer Science Q#96624, answer score: 5
Revisions (0)
No revisions yet.