Tower of Hanoi recursion game algorithm explained
Tower of Hanoi Problem is a mathematical game or puzzle that was invented by the French mathematician Edouard Lucas in 1883. It consists of 3 towers and n numbers of different sizes disks which can easily move on any rod.Why is it called Tower of Hanoi?
The tower of Hanoi was invented by a French mathematician Édouard Lucas in the 19th century. Tower of Hanoi also called the Tower of Brahma or the Lucas tower.
What are the rules of Tower of Hanoi?
Let us understand Tower of Hanoi rules to move all disks over to 3 towers but you can not place a larger disk on a smaller disk but few rules.Tower of Hanoi rules to be followed are −
Only one disk moves at a time.
Only the "top" disk can be removed.
In each movement taking the top disk from one tower to top of another tower but you can not place larger on a smaller disk.
How do you play Tower of Hanoi?
The below image shows the step by step movement of disks from one tower to another and it takes 7 moves for 3 disksHow many moves does it take to solve the Tower of Hanoi?
For finding the number of moves for n disks is 2n-1 is moves required.
For example, for 3 disks 23-1=7 moves required.
What is Tower of Hanoi algorithm?
Here I shared an algorithm for Tower of Hanoi, We solve an algorithm for three towers with name, A for source, B fo destination, C (for help in moving the disks) and N is a number of disks.An Algorithm as follows:
START
Hanoi(A, B, C, N)
IF N<=0
Wrong No. of Disk
ELSE IF N == 1, THEN
move N from A to B // move disk from source to destination
ELSE Hanoi (A, B, C, N-1) // Step 1
move N from A to B // move disk from source to destination
Hanoi (C, B, A, N-1) // Step 3
END IF END
Hanoi(A, B, C, N)
IF N<=0
Wrong No. of Disk
ELSE IF N == 1, THEN
move N from A to B // move disk from source to destination
ELSE Hanoi (A, B, C, N-1) // Step 1
move N from A to B // move disk from source to destination
Hanoi (C, B, A, N-1) // Step 3
END IF END
Exit
What is Tower of Hanoi in C?
C Program to solves the tower of hanoi uses a recursive function. The tower of hanoi is a mathematical puzzle. Click link to learn more about Tower of Hanoi in C
In this above article, we learn about tower of hanoi or we can say tower of hanoi game in a very easy way and also understand the algorithm for the tower of hanoi.