Tower : x z y
Solution of Above Tower of Hanoi Problem will be soon..... using c++
#include<conio.h>
#include<iostream.h>
class tower
{
//Private variables
int NoDisk;
char FromTower,ToTower,AuxTower;
public:
void hanoi(int,char,char,char);
};
void tower::hanoi(int NoDisk,char FromTower,char ToTower, char AuxTower)
{
//if only one disk, make the move and return
if (NoDisk == 1)
{
cout<<"\nMove from disk 1 from tower "<<FromTower<<" to tower "<<ToTower;
return;
}
//Move top n1 disks from X to Y, using Z as aux. tower
hanoi(NoDisk - 1,FromTower,AuxTower,ToTower);
//Move remaining disk from X to Z
cout<<"\nMove from disk "<<NoDisk<<" from tower "<<FromTower<<" to tower "<<ToTower;
//Move n1 disk from Y to Z using X as aux. tower
hanoi(NoDisk - 1,AuxTower,ToTower,FromTower);
return;
}
void main()
{
int No;
tower Ob;
clrscr();
cout<<"\n\t\t\t--- Tower of Hanoi ---\n";
//Imput the number of disk in the tower
cout<<"\n\nEnter the number of disks = ";
cin>>No;
//We assume that the towers are X, Y and Z
Ob.hanoi(No,'X','Z','Y');
getch();
}
Output :
Think Above Solution for Five Ring Tower. and Consider Cases As Below Detail.
Output :
Another Solution without recursion Technique :
Move | Binary Number | Interpretation |
1 | 00001 | Move disk 1 to empty peg. |
2 | 00010 | Move disk 2 to empty peg. |
3 | 00011 | Move disk 1 to cover disk 2. |
4 | 00100 | Move disk 3 to empty peg. |
5 | 00101 | Move disk 1 NOT to cover disk 3. |
6 | 00110 | Move disk 2 to cover disk 3. |
7 | 00111 | Move disk 1 to cover disk 2. |
8 | 01000 | Move disk 4 to empty peg. |
9 | 01001 | Move disk 1 to cover disk 4. |
10 | 01010 | Move disk 2 NOT to cover disk 4. |
11 | 01011 | Move disk 1 to cover disk 2. |
12 | 01100 | Move disk 3 to cover disk 4. |
13 | 01101 | Move disk 1 NOT to cover disk 3. |
14 | 01110 | Move disk 2 to cover disk 3. |
15 | 01111 | Move disk 1 to cover disk 2. |
16 | 10000 | Move disk 5 to empty peg. |
17 | 10001 | Move disk 1 NOT to cover disk 5. |
18 | 10010 | Move disk 2 to cover disk 5. |
19 | 10011 | Move disk 1 to cover disk 2. |
20 | 10100 | Move disk 3 NOT to cover disk 5. |
21 | 10101 | Move disk 1 NOT to cover disk 3. |
22 | 10110 | Move disk 2 to cover disk 3. |
23 | 10111 | Move disk 1 to cover disk 2. |
24 | 11000 | Move disk 4 to cover disk 5. |
25 | 11001 | Move disk 1 to cover disk 4. |
26 | 11010 | Move disk 2 NOT to cover disk 4. |
27 | 11011 | Move disk 1 to cover disk 2. |
28 | 11100 | Move disk 3 to cover disk 4. |
29 | 11101 | Move disk 1 NOT to cover disk 3. |
30 | 11110 | Move disk 2 to cover disk 3. |
31 | 11111 | Move disk 1 to cover disk 2. |
Think Above Solution for Five Ring Tower. and Consider Cases As Below Detail.
Complexity Consider on (2^5 -1)=31
Move | Binary Number | Interpretation |
8 | 01000 | Move disk 4 to empty peg. |
13 | 01011 | Move disk 1 to cover disk 2. |
25 | 11001 | Move disk 1 to cover disk 4. |
18 | 10010 | Move disk 2 to cover disk 5. |
10 | 11010 | Move disk 2 NOT to cover disk 4. |
17 | 10001 | Move disk 1 NOT to cover disk 5. |