You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Q) 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.
2
+
// 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.
3
+
4
+
5
+
6
+
functiontoh(n,fromRod,toRod,auxiliaryRod){
7
+
8
+
letcount=0;
9
+
10
+
functionhelper(n,fromRod,toRod,auxiliaryRod){
11
+
if(n===1){
12
+
count++;
13
+
console.log(`move disk ${n} from rod ${fromRod} to ${toRod}`);
14
+
return;
15
+
}
16
+
17
+
helper(n-1,fromRod,auxiliaryRod,toRod);
18
+
count++;
19
+
console.log(`move disk ${n} from rod ${fromRod} to ${auxiliaryRod}`);
0 commit comments