-
Notifications
You must be signed in to change notification settings - Fork 213
Expand file tree
/
Copy pathTowerOfHanoi.cpp
More file actions
94 lines (72 loc) · 2.12 KB
/
TowerOfHanoi.cpp
File metadata and controls
94 lines (72 loc) · 2.12 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
The tower of Hanoi is a famous puzzle where we have three rods and N disks. The objective of the puzzle is to move the entire stack to another rod.
You are given the number of discs N. Initially, these discs are in the rod 1. You need to print all the steps of discs movement so that all the discs
reach the 3rd rod. Also, you need to find the total moves.
Note: The discs are arranged such that the top disc is numbered 1 and the bottom-most disc is numbered N.
Also, all the discs have different sizes and a bigger disc cannot be put on the top of a smaller disc.
Refer the provided link to get a better clarity about the puzzle.
*/
#include <bits/stdc++.h>
using namespace std;
class Solution{
public:
// You need to complete this function
long long moves=0;
// 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
if(N==1)
{
cout<<"move disk 1 from rod "<<from<<" to rod "<<to<<endl;
moves++;
return moves;
}
toh(N-1, from, aux, to);
moves++;
cout<<"move disk "<<N<<" from rod "<<from<<" to rod "<<to<<endl;
toh(N-1, aux, to, from);
}
};
//{ 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;
}
/*
Example 1:
Input:
N = 2
Output:
move disk 1 from rod 1 to rod 2
move disk 2 from rod 1 to rod 3
move disk 1 from rod 2 to rod 3
3
Explanation: For N=2 , steps will be
as follows in the example and total
3 steps will be taken.
*/
/*
Example 2:
Input:
N = 3
Output:
move disk 1 from rod 1 to rod 3
move disk 2 from rod 1 to rod 2
move disk 1 from rod 3 to rod 2
move disk 3 from rod 1 to rod 3
move disk 1 from rod 2 to rod 1
move disk 2 from rod 2 to rod 3
move disk 1 from rod 1 to rod 3
7
Explanation: For N=3 , steps will be
as follows in the example and total
7 steps will be taken.
*/