Tower of Hanoi is a mathematical puzzle consisting of 3 pegs / towers [ A, B, C ] and some disks of varying diameter.
Below is the example of solving the Tower Of Hanoi puzzle with 3 disks.
It can be mathematically proved that the minumum number of moves required to move N disks is 2 N - 1.
Idea : The idea behind recursion is to move the top ( N - 1 ) disks from source peg to auxiliary peg. The bottom-most disk that is now left on the source peg is then moved to the desitnation peg. Thus the rules of the puzzle are obeyed and we get the below recursive algorithm for solving the puzzle of Tower Of Hanoi.
Algorithm : MoveDisks ( Integer disks, String source_peg, String using_peg, String dest_peg )
1. If there is just 1 disk then
Move disk 1 from source_peg to dest_peg. ( w/o using any auxiliary peg )
2. Else
3. First move the top ( N - 1 ) disks from source_peg to aux_peg using dest_peg ( which is used as an auxiliary peg ).
i.e MoveDisks ( N - 1, source_peg, dest_peg, aux_peg ).
4. Move the bottom-most disk N left on the source_peg to dest_peg.
5. Now that we have N - 1 disks on aux_peg, we could move them to dest_peg using source_peg ( as auxiliary peg )
i.e MoveDisks ( N - 1, aux_peg, source_peg, dest_peg ).
def MoveDisks ( disks, src_peg, aux_peg, dest_peg ) :
if ( disks == 1 ) :
print("Move disk 1 from " + src_peg + " to " + dest_peg)
else :
# First move the top N-1 disks to aux_peg using dest_peg ( as auxiliary peg )
MoveDisks ( disks-1, src_peg, dest_peg, aux_peg )
# Move the bottom-most disk to the destination peg.
print ( "Move disk " + str(disks) + " from " + src_peg + " to " + dest_peg )
# We have N-1 disks ( moved in previous step ) on aux_peg. So move these disks from aux_peg
# to dest_peg using src_peg ( as auxiliary peg ) by recursively calling MoveDisks
MoveDisks ( disks-1, aux_peg, src_peg, dest_peg )
def main() :
disks = 3
MoveDisks(disks, "A", "B", "C")
if __name__ == "__main__":
main()
Output
Move disk 1 from A to C
Move disk 2 from A to B
Move disk 1 from C to B
Move disk 3 from A to C
Move disk 1 from B to A
Move disk 2 from B to C
Move disk 1 from A to C
#include<iostream>
#include<string>
using namespace std;
void MoveDisks ( int disks, string src_peg, string aux_peg, string dest_peg ) {
if ( disks == 1 ) {
cout << "Move disk 1 from " << src_peg << " to " << dest_peg << endl;
} else {
// First move the top N-1 disks to aux_peg using dest_peg ( as auxiliary peg )
MoveDisks ( disks-1, src_peg, dest_peg, aux_peg );
// Move the bottom-most disk to the destination peg.
cout << "Move disk " << disks << " from " << src_peg << " to " << dest_peg << endl;
// We have N-1 disks ( moved in previous step ) on aux_peg. So move these disks from aux_peg
// to dest_peg using src_peg ( as auxiliary peg ) by recursively calling MoveDisks
MoveDisks ( disks-1, aux_peg, src_peg, dest_peg );
}
}
int main() {
int disks = 3;
MoveDisks(disks, "A", "B", "C");
return 0;
}
Output
Move disk 1 from A to C
Move disk 2 from A to B
Move disk 1 from C to B
Move disk 3 from A to C
Move disk 1 from B to A
Move disk 2 from B to C
Move disk 1 from A to C
class TowerOfHanoi {
void MoveDisks ( int disks, String src_peg, String aux_peg, String dest_peg ) {
if ( disks == 1 ) {
System.out.println("Move disk 1 from " + src_peg + " to " + dest_peg);
} else {
// First move the top N-1 disks to aux_peg using dest_peg ( as auxiliary peg )
MoveDisks ( disks-1, src_peg, dest_peg, aux_peg );
// Move the bottom-most disk to the destination peg.
System.out.println("Move disk " + disks + " from " + src_peg + " to " + dest_peg);
// We have N-1 disks ( moved in previous step ) on aux_peg. So move these disks from aux_peg
// to dest_peg using src_peg ( as auxiliary peg ) by recursively calling MoveDisks
MoveDisks ( disks-1, aux_peg, src_peg, dest_peg );
}
}
public static void main (String[] args) {
TowerOfHanoi t = new TowerOfHanoi();
int disks = 3;
t.MoveDisks(disks, "A", "B", "C");
}
}
Output
Move disk 1 from A to C
Move disk 2 from A to B
Move disk 1 from C to B
Move disk 3 from A to C
Move disk 1 from B to A
Move disk 2 from B to C
Move disk 1 from A to C