- #1
mathmari
Gold Member
MHB
- 5,049
- 7
Hey!
The Towers of Hanoi problem consists of three pegs $A$, $B$ and $C$, and $n$ squares of varying size. Initially the squares are stacked on peg$A$ in order of decreasing size, the largest square on the bottom. The problem is to move the squares from peg$A$ to peg$B$ one at a time in such away that no square is ever placed on a smaller square. Peg$C$ may be used for temporary storage of squares.
I have to write an algorithm to solve this problem.
I made an example for $n=3$. The moves are the following:
right?? (Wondering)
How could we write it as an algorithm?? Maybe using recursion?? But how?? (Wondering)
The Towers of Hanoi problem consists of three pegs $A$, $B$ and $C$, and $n$ squares of varying size. Initially the squares are stacked on peg$A$ in order of decreasing size, the largest square on the bottom. The problem is to move the squares from peg$A$ to peg$B$ one at a time in such away that no square is ever placed on a smaller square. Peg$C$ may be used for temporary storage of squares.
I have to write an algorithm to solve this problem.
I made an example for $n=3$. The moves are the following:
Code:
Move the square from pegA to pegB
Move the square from pegA to pegC
Move the square from pegB to pegC
Move the square from pegA to pegB
Move the square from pegC to pegA
Move the square from pegC to pegB
Move the square from pegA to pegB
right?? (Wondering)
How could we write it as an algorithm?? Maybe using recursion?? But how?? (Wondering)