Tower of Hanoi is a mathematical puzzle. It consists of three poles and a number of disks of different sizes which can slide onto any poles. The puzzle starts with the disk in a neat stack in ascending order of size in one pole, the smallest at the top thus making a conical shape. The objective of the puzzle is to move all the disks from one pole (say ‘source pole’) to another pole (say ‘destination pole’) with the help of the third pole (say auxiliary pole).
The puzzle has the following two rules:
1. You can’t place a larger disk onto smaller disk
2. Only one disk can be moved at a time
We’ve already discussed recursive solution for Tower of Hanoi. We have also seen that, for n disks, total 2n – 1 moves are required.
1. Calculate the total number of moves required i.e. "pow(2, n) - 1" here n is number of disks. 2. If number of disks (i.e. n) is even then interchange destination pole and auxiliary pole. 3. for i = 1 to total number of moves: if i%3 == 1: legal movement of top disk between source pole and destination pole if i%3 == 2: legal movement top disk between source pole and auxiliary pole if i%3 == 0: legal movement top disk between auxiliary pole and destination pole
Let us understand with a simple example with 3 disks: So, total number of moves required = 7 S A D When i= 1, (i % 3 == 1) legal movement between‘S’ and ‘D’ When i = 2, (i % 3 == 2) legal movement between ‘S’ and ‘A’ When i = 3, (i % 3 == 0) legal movement between ‘A’ and ‘D’ ’ When i = 4, (i % 3 == 1) legal movement between ‘S’ and ‘D’ When i = 5, (i % 3 == 2) legal movement between ‘S’ and ‘A’ When i = 6, (i % 3 == 0) legal movement between ‘A’ and ‘D’ When i = 7, (i % 3 == 1) legal movement between ‘S’ and ‘D’
So, after all these destination poles contains all the in order of size.
After observing above iterations, we can think that after a disk other than the smallest disk is moved, the next disk to be moved must be the smallest disk because it is the top disk resting on the spare pole and there are no other choices to move a disk.
Move the disk 1 from 'S' to 'D' Move the disk 2 from 'S' to 'A' Move the disk 1 from 'D' to 'A' Move the disk 3 from 'S' to 'D' Move the disk 1 from 'A' to 'S' Move the disk 2 from 'A' to 'D' Move the disk 1 from 'S' to 'D'
This article is contributed by Anand Barnwal. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Program for Tower of Hanoi
- Cost Based Tower of Hanoi
- Iterative Postorder traversal | Set 3
- Iterative Postorder Traversal of N-ary Tree
- Iterative Postorder Traversal | Set 1 (Using Two Stacks)
- Iterative Depth First Traversal of Graph
- Iterative Postorder Traversal | Set 2 (Using One Stack)
- Iterative Fast Fourier Transformation for polynomial multiplication
- Iterative method to find ancestors of a given binary tree
- Print the last k nodes of the linked list in reverse order | Iterative Approaches
- Minimum cost of reducing Array by merging any adjacent elements repetitively
- Count of unordered pairs (x, y) of Array which satisfy given equation
- Divide N into K parts in the form (X, 2X, ... , KX) for some value of X
- Check given string is oddly palindrome or not | Set 2