forked from harshitbansal373/c-plus-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tower_of_hanoi_ring-illustraion_in_code.cpp
70 lines (62 loc) · 1.35 KB
/
tower_of_hanoi_ring-illustraion_in_code.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
#include<iostream>
using namespace std;
void build(int a[][3],int n){
int i,j;
for(i=0;i<n;i++){
a[i][0]=i+1;
for(j=1;j<3;j++){
a[i][j]=0;
}
}
}
void view(int a[][3],int n){
int i,j;
cout<<endl<<"----------------------------------------------"<<endl;
for(i=0;i<n;i++){
for(j=0;j<3;j++){
cout<<a[i][j];
}
cout<<endl;
}
}
void mover(int d,int a[][3],int from,int to,int n){
int i,j,k;
for(i=n-1;i>=0;i--){
if(a[i][from]==0){
continue;
}
else{
d=i;
}
}
for(i=n-1;i>=0;i--){
if(a[i][to]!=0){
continue;
}
else{
k=i;
break;
}
}
a[k][to]=a[d][from];
a[d][from]=0;
}
void tower(int n,int a[][3],int source,int spare,int desti,int row){
if(n!=0){
tower(n-1,a,source,desti,spare,row);
mover(n-1,a,source,desti,row);
view(a,row);
tower(n-1,a,spare,source,desti,row);
}
}
int main(){
int i,j,n,r;
cout<<endl<<"Enter the no of rings :";
cin>>n;
r=n;
int a[n][3];
build(a,n);
view(a,n);
tower(n,a,0,1,2,r);
return 0;
}