-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTowerOfHanoi.cpp
107 lines (103 loc) · 2.71 KB
/
TowerOfHanoi.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Tower Of Hanoi
#include<stack>
#include<iostream>
using namespace std;
void display(stack<int> s)
{
if(s.empty())
{
cout<<" - "<<endl;
cout<<" ---"<<endl;
cout<<"-----"<<endl;
}
if(!s.empty())
{
while(!s.empty())
{
cout<<s.top()<<endl;
s.pop();
}
}
}
int main()
{
stack<int> tower1, tower2, tower3;
int ring1;
cout<<"Enter size of 1st ring (the largest one) : "<<endl;
cin>>ring1;
tower1.push(ring1);
int ring2;
cout<<"Enter size of 2nd ring (the medium one) : "<<endl;
cin>>ring2;
tower1.push(ring2);
int ring3;
cout<<"Enter size of 3rd ring (the smallest one) : "<<endl;
cin>>ring3;
tower1.push(ring3);
// Shows Initial Position :
cout<<"****************************************Initial Problem : *************************************"<<endl;
cout<<"Tower 1 :"<<endl;
display(tower1);
cout<<"Tower 2 :"<<endl;
display(tower2);
cout<<"Tower 3 :"<<endl;
display(tower3);
// Step-1 :
cout<<"**************************************Step-1 : ****************************************"<<endl;
int element = tower1.top();
tower1.pop();
tower3.push(element);
element = tower1.top();
tower2.push(element);
tower1.pop();
// Show Step-1 :
cout<<"Tower 1 :"<<endl;
display(tower1);
cout<<"Tower 2 :"<<endl;
display(tower2);
cout<<"Tower 3 :"<<endl;
display(tower3);
// Step-2 :
cout<<"******************************************* Step-2 : ****************************************"<<endl;
element = tower3.top();
tower3.pop();
tower2.push(element);
element = tower1.top();
tower1.pop();
tower3.push(element);
// Show Step-2 :
cout<<"Tower 1 :"<<endl;
display(tower1);
cout<<"Tower 2 :"<<endl;
display(tower2);
cout<<"Tower 3 :"<<endl;
display(tower3);
// Step-3 :
cout<<"************************************** Step-3 : *******************************************"<<endl;
element = tower2.top();
tower2.pop();
tower1.push(element);
element = tower2.top();
tower2.pop();
tower3.push(element);
// Show Step-3 :
cout<<"Tower 1 :"<<endl;
display(tower1);
cout<<"Tower 2 :"<<endl;
display(tower2);
cout<<"Tower 3 :"<<endl;
display(tower3);
// Step-4 :
cout<<"***************************************** Step-4 *****************************************"<<endl;
element = tower1.top();
tower1.pop();
tower3.push(element);
// Show Step-4 :
cout<<"Tower 1 :"<<endl;
display(tower1);
cout<<"Tower 2 :"<<endl;
display(tower2);
cout<<"Tower 3 :"<<endl;
display(tower3);
return 0;
}