Tower of Hanoi Algorithm
Tower of Hanoi Algorithm
- Pseudocode:
Tower of Hanoi
Input: an integer n = number of disks, three chars {source,
inter, destination}.
Output: steps of solution (every move), number of moves.
START
Counter 0 \\num of moves
tower (n: disk, source, inter, destination)
IF n = 1, THEN
Counter Counter + 1
Print “move disk from “ + source + “to ” + destination
ELSE
Counter Counter + 1
tower (disk - 1, source, destination, inter)
Print “move disk from “+ source +”to “ + destination
tower (disk - 1, inter, source, destination)
END IF
END
Return (Counter)
- Java Code:
The following image is an example of the Hanoi of Tower and it is applied in the
previous Java code