Tower of Hanoi
•Tower of Hanoi is a mathematical puzzle invented by a French
•The game starts by having few discs stacked in increasing order of
size. The number of discs can vary, but there are only three pegs.
•The Objective is to transfer the entire tower to one of the other
pegs. However, you can only move one disk at a time and you can
never stack a larger disk onto a smaller disk. Try to solve it in
How to solve the 4 pegs
Tower of Hanoi
Recursive Solution for the Tower of Hanoi with algorithm
Let’s call the three peg Src(Source), Aux(Auxiliary) and
Dst(Destination).
1)Move the top N – 1 disks from the Source to Auxiliary tower
2)Move the Nth disk from Source to Destination tower
3)Move the N – 1 disks from Auxiliary tower to Destination tower.
Transferring the top N – 1 disks from Source to Auxiliary tower
can again be thought of as a fresh problem and can be solved in the
same manner.
Algorithm
TOWER(N, BEG, AUX, END)
1.If N = 1 then
a.write BEG --> END
b.Return
2.[Move N-1 disks from peg BEG to peg AUX]
Call TOWER(N-1, BEG, END, AUX)
3. Write BEG --> END
4.[Move N-1 disks from peg AUX to peg END]
Call TOWER(N-1, AUX, BEG, END)
5.Return.
For N = 4 we get the following sequence
1. Move from Src to Aux
2. Move from Src to Dst
3. Move from Aux to Dst
4. Move from Src to Aux
5. Move from Dst to Src
6. Move from Dst to Aux
7. Move from Src to Aux
8. Move from Src to Dst
9. Move from Aux to Dst
10. Move from Aux to Src
11. Move from Dst to Src
12. Move from Aux to Dst
13. Move from Src to Aux
14. Move from Src to Dst
15. Move from Aux to Dst
How many moves will it take to transfer n
disks from the left post to the right post?
•for 1 disk it takes 1 move to transfer 1 disk from post A to
post C;
•for 2 disks, it will take 3 moves: 2M + 1 = 2(1) + 1 = 3
•for 3 disks, it will take 7 moves: 2M + 1 = 2(3) + 1 = 7
•for 4 disks, it will take 15 moves: 2M + 1 = 2(7) + 1 = 15
•for 5 disks, it will take 31 moves: 2M + 1 = 2(15) + 1 = 31
•for 6 disks... ?
•Explicit Pattern
•Number of Disks Number of Moves
1 1
2 3
3 7
4 15
5 31
1 1
2 3
3 7
4 15
5 31
• Powers of two help reveal the pattern:
•Number of Disks (n) Number of Moves
1 2^1 - 1 = 2 - 1 = 1
2 2^2 - 1 = 4 - 1 = 3
3 2^3 - 1 = 8 - 1 = 7
4 2^4 - 1 = 16 - 1 = 15
5 2^5 - 1 = 32 - 1 = 31
1 2^1 - 1 = 2 - 1 = 1
2 2^2 - 1 = 4 - 1 = 3
3 2^3 - 1 = 8 - 1 = 7
4 2^4 - 1 = 16 - 1 = 15
5 2^5 - 1 = 32 - 1 = 31
Coding for the implementation of Tower of Hanoi
#include<iostream>
#include<conio.h>
#include<iomanip>
int count=0;
using namespace std;
void TOH(int d, char tower1, char tower2, char tower3)
{
if(d==1)
{
count++;
cout<<"\n shift top disk from tower"<<tower1<<" to tower"<<tower2;
return;
}
TOH(d-1,tower1,tower3,tower2);
cout<<"\n shift top disk from tower"<<tower1<<" to tower"<<tower2;
count++;
TOH(d-1,tower3,tower2,tower1);
}
int main()
{
int disk;
cout<<"enter total number of disks";
cin>>disk;
if (disk<1)
{
cout<<"there is no disk to shift";
}
else
{
cout<<"there is "<<disk<<" disks in towwer 1 to shift to tower2";
}
TOH(disk,'1','2','3');
cout<<endl<<endl<<disk<<" disks in tower1 ate shifted to tower 2 in "<<count<<" attempts";
getch();
return 0;
}
0 Comments