Tower of Hanoi

The Tower of Hanoi or Towers of Hanoi, also called the Tower of Brahma or Towers of Brahma, is a mathematical game or puzzle. It consists of three pegs, and a number of disks of different sizes which can slide onto any peg. The puzzle starts with the disks in a neat stack in ascending order of size on one peg, the smallest at the top, thus forming a pyramid.

The objective of the puzzle is to move the entire stack to another peg, obeying the following rules:

Traguardo dell'esercizio

Write the core of the method: [!java|c]void [/!]solve[!c]Rec[/!]([!java|c]int [/!]src[!scala]:Int[/!], [!java|c]int [/!]dst[!scala]:Int[/!],[!java|c]int [/!]other[!scala]:Int[/!], [!java|c]int [/!]height[!scala]:Int[/!])

This method will recursively solve the presented problem. The first parameter named src is the index of the initial tower, the second parameter dst is the index of the expected final tower, the third parameter other is the index of the unused peg while the fourth parameter height is the height of the tower.

A key to solving this puzzle is to recognize that it can be solved by breaking the problem down into a collection of smaller problems and further breaking those problems down into even smaller problems until a solution is reached.

Here is the pseudo-code of the solution: To move n discs from peg A to peg C: