-
Notifications
You must be signed in to change notification settings - Fork 46
/
tower_of_hanoi.cpp
58 lines (48 loc) · 1.11 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
58
#include <bits/stdc++.h>
using namespace std;
long long steps = 0;
void TOH(int n, int a, int b, int c)
{
if (n == 1)
{
steps++;
cout << "move disk " << n << " from rod " << a << " to rod " << b << endl;
return;
}
else// N=2;
{
TOH(n - 1, a, c, b);
steps++;//STEP=1,
cout << "move disk " << n << " from rod " << a << " to rod " << b << endl;// DISK 1-ROD 1 TO ROD 2
TOH(n - 1, c, b, a);
// cout << "move disk " << n << " from rod " << a << " to rod " << b << endl;
}
}
// } Driver Code Ends
class Solution
{
public:
// You need to complete this function
// avoid space at the starting of the string in "move disk....."
long long toh(int N, int from, int to, int aux)
{
// Your code here
TOH(N, from, to, aux);
return steps;
}
};
// { Driver Code Starts.
int main()
{
int T;
cin >> T; // testcases
while (T--)
{
int N;
cin >> N; // taking input N
// calling toh() function
Solution ob;
cout << ob.toh(N, 1, 3, 2) << endl;
}
return 0;
}