Have you heard about this tower of Hanoi problem? you can find this in the syllabus of data structure. If you are not aware of this problem then do not panic I will explain to you that what is the problem of the tower of Hanoi and how to solve this problem.
Within this blog post, we are going to share about How long does it take to solve the Tower of Hanoi? How many moves does it take to solve the Tower of Hanoi for 3 disks? How many moves does it take to solve the Tower of Hanoi for 4 disks? Tower of Hanoi solution, Tower of Hanoi recursive solution.
What exactly the Tower of Hanoi problem is?
The tower of Hanoi problem is the problem to shift a few disks from one peg to another by using a few more pegs in between for help. Peg is noting but a pole where we insert the disk. And this process will be having some conditions like:
- Only the top disk may be moved at a time.
- Only smaller disk place on a larget disk
If you still could not get this tower of Hanoi problem then I make it more simple so that you can understand it in a better way.
See, the above image here we have three pegs A, B, C, and five disks are inserted into peg A in decreasing order. Mean to say that the largest disk is at the bottom and the smallest is at the top.
Now the tower of Hanoi problem or main objective is that we need to move disk from peg A to peg C using peg B as a temporary by following the given below rule.
We can move only the top disk at a time. And another one is that we can place the only smaller disk on a larger disk. That is means we can not put a larger disk over smaller ones at any move of a disk. A disk may be moved from any peg to any other peg.
How to solve the Tower of Hanoi problem? A solution of the tower of Hanoi problem for n=3 disk?
So, now you would have understood the problem of the tower of Hanoi. Now I show you how to solve this problem for three disks or n=3. See the below image for the same.
You can see all the moves to get the final solution of the tower of Hanoi in the above-given image.
In the first image, we have all the three disks in pole A in decreasing order, and then our objective is to shift all these disks to tower C in the same order of placement now means biggest disk at the bottom and smallest disk at the top. We can take the temporary help of pole B in solving this problem.
Now we start moving the disk and first we move the top Ist disk from A and keep it at pole C. After this we move the IInd disk from A and keep it at pole B.
Now we move the Ist disk from pole C and put it at pole B. After this, we move IIIrd disk from A and put it at pole C. We move the Ist disk from B to pole A.
Now we move the IInd disk from B to pole C. And after this, we move the Ist disk from A to C. And our object as per the tower of Hanoi problem has been achieved. So this was the overall solution of the tower of Hanoi with the use of three disks.
How to write the Tower of Hanoi Algorithm? / Tower of Hanoi algorithm in C?
An algorithm is nothing but a set of instructions or statements to complete a program. See the Tower of Hanoi Algorithm below.
1. Given n disk on peg A.
2. Repeat step 3 until no disk is left.
3. Move one disk at a time, such that no larger disk can be placed over a smaller disk.
Write a C program for the Tower of Hanoi? / Tower of Hanoi algorithm in C?
See the c program for a tower of Hanoi below.
void hanoi(int n, char initial, char final, char temp)
printf(“move disk 1 from peg % c to % c\n”,initial, final);
hanoi(n-1, initial, temp, final);
printf(“move disk%d from peg %c to %c\n”,n, initial, final);
hanoi(n-1, temp, final, initial);
printf(“Enter the number of disk to be moved”);
hanoi(n, ‘A’, ‘C’, ‘B’);
The tower of Hanoi problem is the problem of pole disks and their displacement or movement as per the rule. The Tower of Hanoi has three pegs or poles and there could be n number of the disk. In the initial few disks are kept in either pole in descending order and then we need to move all these disks to another peg in the same descending order with the help of the third pole and following some sets of rules like We can move only the top disk at a time and smaller disk always is above of larger disk.
Using this blog post we have gone through How long does it take to solve the Tower of Hanoi? How many moves does it take to solve the Tower of Hanoi for 3 disks? How many moves does it take to solve the Tower of Hanoi for 4 disks? Tower of Hanoi solution, Tower of Hanoi recursive solution.
In the case of any queries, you can write to us at [email protected] we will get back to you ASAP.
Hope! you would have enjoyed this post.
Please feel free to give your important feedbacks in the comment section below.
Have a great time! Sayonara!