forked from rahulbarman03/hackoctoberfest22
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTower_Of_Hanoi_Code.cpp
More file actions
39 lines (33 loc) · 837 Bytes
/
Copy pathTower_Of_Hanoi_Code.cpp
File metadata and controls
39 lines (33 loc) · 837 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
30
31
32
33
34
35
36
37
38
39
// C++ recursive function to
// solve tower of hanoi puzzle
// C++ recursive function to
// solve tower of hanoi puzzle
#include <bits/stdc++.h>
using namespace std;
void towerOfHanoi(int n, char from_rod, char to_rod,
char aux_rod)
{
if (n == 0) {
return;
}
towerOfHanoi(n - 1, from_rod, aux_rod, to_rod);
cout << "Move disk " << n << " from rod " << from_rod
<< " to rod " << to_rod << endl;
towerOfHanoi(n - 1, aux_rod, to_rod, from_rod);
}
// Driver code
int main()
{
int N = 3;
// A, B and C are names of rods
towerOfHanoi(N, 'A', 'C', 'B');
return 0;
}
// Output
// Move disk 1 from rod A to rod C
// Move disk 2 from rod A to rod B
// Move disk 1 from rod C to rod B
// Move disk 3 from rod A to rod C
// Move disk 1 from rod B to rod A
// Move disk 2 from rod B to rod C
// Move disk 1 from rod A to rod C