forked from VivekDubey9/Competitive-Programming-Algos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTowerOfHanoi.cpp
More file actions
29 lines (27 loc) · 744 Bytes
/
TowerOfHanoi.cpp
File metadata and controls
29 lines (27 loc) · 744 Bytes
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
#include <iostream>
using namespace std;
int ToH(int n, char s = 'S', char d = 'D', char h = 'H') //Represented as No. of plate, Source's plate, destination's plate, helper's plate
{
static int count = 0;
if (n == 1)
{
cout << "Moving " << n << "th plate from " << s << " to " << d << ".\n";
count++;
return 0;
}
ToH(n - 1, s, h, d);
cout << "Moving " << n << "th plate from " << s << " to " << d << ".\n";
count++;
ToH(n - 1, h, d, s);
return count;
}
int main()
{
int num;
cout<<"Total number of Plates: ";
cin>>num;
long long int cnt = ToH(num);
cout<<"\nFor "<<num<<" plates ";
cout << cnt<<" Steps required!";
return 0;
}