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 % 4 == 1) legal movement between ‘S’ and ‘D’ When i = 5, (i % 5 == 2) legal movement between ‘S’ and ‘A’ When i = 6, (i % 6 == 0) legal movement between ‘A’ and ‘D’ When i = 7, (i % 7 == 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
- Program for Tower of Hanoi
- Iterative Postorder Traversal | Set 2 (Using One Stack)
- Iterative Depth First Traversal of Graph
- Iterative Postorder Traversal | Set 1 (Using Two Stacks)
- Iterative method to find ancestors of a given binary tree
- Iterative Fast Fourier Transformation for polynomial multiplication
- Search element in a Spirally sorted Matrix
- Merge K sorted arrays of different sizes | ( Divide and Conquer Approach )
- Generate a random permutation of elements from range [L, R] (Divide and Conquer)
- Print Stack Elements from Bottom to Top
- Merge K sorted arrays | Set 3 ( Using Divide and Conquer Approach )
- Program to reverse a linked list using Stack
- Sum of minimum elements of all possible sub-arrays of an array
- Sum of maximum elements of all possible sub-arrays of an array