-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack_HanoiTower.cpp
65 lines (49 loc) · 1.33 KB
/
Stack_HanoiTower.cpp
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream.>
#include <conio.h>
using namespace std;
int count;
class tower
{
//int NoDisk;
char FromTower, ToTower, AuxTower;
public:
void hanoi(int, char, char, char);
};
void tower::hanoi(int NoDisk, char FromTower, char ToTower, char AuxTower)
{
count+=1;
// if only one disk, make the move and return
if(NoDisk == 1)
{
cout<<"\n Move disk 1 from tower "<<FromTower<<" to tower "<<ToTower;
return;
}
// move top n-1 disks form X to Y, using Z as auxiliary tower
hanoi(NoDisk-1, FromTower, AuxTower, ToTower);
// move remaining disk form x to z
cout<<"\n Move disk "<<NoDisk<<" from tower "<<FromTower<<" to tower "<<ToTower;
//move n-1 disk form Y to Z using X as auxiliry tower
hanoi(NoDisk-1, AuxTower, ToTower, FromTower);
return;
}
int main(){
int No;
char option;
tower obj;
//clrscr();
cout<<"\n \t \t \t --- Tower of Hanoi --- \n ";
do{
count = 0;
// input the number of disk in the tower
cout<<"\n Enter the nubmer of disk: ";
cin>>No;
// we assume that the towers are X, Y and Z
obj.hanoi(No, 'X', 'Z', 'Y');
cout<<"\n count : "<<count;
cout<<"\n Do U want to repeat the Tower of Hanoi? Yes(y), No(n): ";
cin>>option;
}while(option == 'y');
cout<<"\n press any key to exit... ";
getch();
return 0;
}