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 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 pole 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
- Print next greater number of Q queries
- Check if a given array can represent Preorder Traversal of Binary Search Tree
- Sort a stack using recursion
- Length of the longest valid substring
- Find maximum of minimum for every window size in a given array
- How to efficiently implement k stacks in a single array?
- Stack | Set 4 (Evaluation of Postfix Expression)
- Program for Tower of Hanoi
- Design a stack with operations on middle element
- Expression Evaluation
- Largest Rectangular Area in a Histogram | Set 2
- The Stock Span Problem
- The Celebrity Problem
- Reverse a stack using recursion
- Check for balanced parentheses in an expression
Improved By : VIKASGUPTA1127