forked from MAYANK25402/Hactober-2023-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TowerOfHanoi.cpp
49 lines (44 loc) · 912 Bytes
/
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
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define forn(i, n) for (int i = 0; i < int(n); i++)
// freopen('input.txt', 'r', stdin);
// freopen('output.txt', 'w', stdout);
ll power(int base, ll exp)
{
ll ans = 1;
for (ll i = 0; i < exp; i++)
{
ans *= base;
}
return ans;
}
void TOH(int a, int b, int c, int n)
{
if (n == 1)
{
cout << a << " " << c << endl;
return;
}
TOH(a, c, b, n - 1); // src to auxiliary
cout << a << " " << c << endl;
TOH(b, a, c, n - 1); // auxiliary to destination
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
cout << power(2, n) - 1 << endl;
TOH(1, 2, 3, n);
return 0;
}