-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
Copy pathTowerOfHanoi.js
26 lines (24 loc) · 1000 Bytes
/
TowerOfHanoi.js
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
// wiki - https://en.wikipedia.org/wiki/Tower_of_Hanoi
// Recursive Javascript function to solve tower of hanoi
/**
* Solves the Tower of Hanoi problem recursively.
* @param {number} n - The number of disks to move.
* @param {string} from - The rod from which to move the disks.
* @param {string} to - The rod to which to move the disks.
* @param {string} aux - The auxiliary rod for moving disks.
* @param {string[]} [output=[]] - Optional array to store the sequence of moves.
* @returns {string[]} The sequence of moves to solve the Tower of Hanoi problem.
*/
export function TowerOfHanoi(n, from, to, aux, output = []) {
if (n === 1) {
output.push(`Move disk 1 from rod ${from} to rod ${to}`)
return output
}
TowerOfHanoi(n - 1, from, aux, to, output)
output.push(`Move disk ${n} from rod ${from} to rod ${to}`)
TowerOfHanoi(n - 1, aux, to, from, output)
return output
}
// Driver code (A, C, B are the name of rods)
// const n = 4
// TowerOfHanoi(n, 'A', 'C', 'B')