Tower of Hanoi Problem
The Tower of Hanoi is a mathematical puzzle consisting of three rods and n disks of different sizes which can slide onto any rod.
The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the smallest at the top, making a conical shape. The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules:
- Only one disk can be moved at a time.
- Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack, i.e., a disk can only be moved if it is the uppermost disk on a stack.
- No disk may be placed on top of a smaller disk.
The minimum number of moves required to solve a Tower of Hanoi puzzle is 2n-1, where n is the total number of disks.
An animated solution of the Tower of Hanoi puzzle for n = 4 can be seen here. Following are the steps that were taken by the proposed solution:
- Move disk 1 from 1 to 2
- Move disk 2 from 1 to 3
- Move disk 1 from 2 to 3
- Move disk 3 from 1 to 2
- Move disk 1 from 3 to 1
- Move disk 2 from 3 to 2
- Move disk 1 from 1 to 2
- Move disk 4 from 1 to 3
- Move disk 1 from 2 to 3
- Move disk 2 from 2 to 1
- Move disk 1 from 3 to 1
- Move disk 3 from 2 to 3
- Move disk 1 from 1 to 2
- Move disk 2 from 1 to 3
- Move disk 1 from 2 to 3
We can easily solve the Tower of Hanoi problem using recursion. A key to solving this puzzle is recognizing that we can solve it by breaking the problem down into a collection of smaller problems and further breaking those problems down into even smaller problems until a solution is reached.
To move n discs from pole 1 to pole 3:
- Move
n-1discs from 1 to 2. This leaves discnalone on pole 1. - Move disc
nfrom 1 to 3. - Move
n-1discs from 2 to 3, so they sit on discn.
The algorithm can be implemented as follows in C++, Java, and Python:
C++
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include <iostream> using namespace std; void move(int disks, int source=1, int auxiliary=2, int target=3) { if (disks > 0) { // move `n-1` discs from source to auxiliary using the target // as an intermediate pole move(disks - 1, source, target, auxiliary); // move one disc from source to target cout << "Move disk " << disks << " from " << source << " —> " << target << endl; // move `n-1` discs from auxiliary to target using the source // as an intermediate pole move(disks - 1, auxiliary, source, target); } } int main() { int n = 3; move(n); return 0; } |
Java
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
class Main { public static void move(int disks, int source, int auxiliary, int target) { if (disks > 0) { // move `n-1` discs from source to auxiliary using the target // as an intermediate pole move(disks - 1, source, target, auxiliary); // move one disc from source to target System.out.println("Move disk " + disks + " from " + source + " —> " + target); // move `n-1` discs from auxiliary to target using the source // as an intermediate pole move(disks - 1, auxiliary, source, target); } } // Tower of Hanoi Problem public static void main(String[] args) { int n = 3; move(n, 1, 2, 3); } } |
Python
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
def move(disks, source=1, auxiliary=2, target=3): if disks > 0: # move `n-1` discs from source to auxiliary using the target # as an intermediate pole move(disks - 1, source, target, auxiliary) # move one disc from source to target print(f'Move disk {disks} from {source} —> {target}') # move `n-1` discs from auxiliary to target using the source # as an intermediate pole move(disks - 1, auxiliary, source, target) # Tower of Hanoi Problem if __name__ == '__main__': n = 3 move(n) |
Move disk 1 from 1 —> 3
Move disk 2 from 1 —> 2
Move disk 1 from 3 —> 2
Move disk 3 from 1 —> 3
Move disk 1 from 2 —> 1
Move disk 2 from 2 —> 3
Move disk 1 from 1 —> 3
The time complexity of the above solution is O(2n), where n is the total number of disks. If n = 64 and one disk is moved per second, then using the smallest number of moves available will take 264-1 seconds, or roughly 585 billion years to finish the game.
References: https://en.wikipedia.org/wiki/Tower_of_Hanoi
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)
