-
Notifications
You must be signed in to change notification settings - Fork 6
/
Tower of Hanoi.cpp
57 lines (42 loc) · 1.51 KB
/
Tower of Hanoi.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
#include<bits/stdc++.h>
#define PI acos(-1)
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
#define lli long long int
#define ull unsigned long long int
#define pii pair < int, int>
#define pll pair < ll, ll>
#define sc scanf
#define scin(x) sc("%d",&(x))
#define scln(x) sc("%lld",&(x))
#define pf printf
#define ms(a,b) memset(a,b,sizeof(a))
#define veci vector<int>
#define vecl vector<long long int>
#define pb push_back
using namespace std ;
long long int m=0;
void towers(int num, char frompeg, char topeg, char auxpeg)
{
if (num == 1)
{
printf("\n Move disk 1 from peg %c to peg %c", frompeg, topeg); //move 1 disk from A to C
m++;
return;
}
towers(num - 1, frompeg, auxpeg, topeg); //move n-1 disks from A to B using C
printf("\n Move disk %d from peg %c to peg %c", num, frompeg, topeg);
m++;
towers(num - 1, auxpeg, topeg, frompeg); //move n-1 disks from B to C using A
}
int main()
{
int num;
cout << "Enter the number of disks : " << endl;
cin >> num ;
towers(num, 'A', 'C', 'B');
cout <<endl << " Number of movements = " << m << endl;
return 0;
}