Tower of Hanoi
Medium

Problem Statement

The Tower of Hanoi is a mathematical puzzle where we have three rods and N disks of different sizes. The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the smallest at the top. The objective is to move the entire stack to another rod, obeying simple rules.

Examples

1Example 1
Input:
{ "N": 2, "from": "A", "to": "C", "aux": "B" }
Output:
[ "move disk 1 from rod A to rod B", "move disk 2 from rod A to rod C", "move disk 1 from rod B to rod C" ]
2Example 2
Input:
{ "N": 3, "from": "A", "to": "C", "aux": "B" }
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" ]
3Example 3
Input:
{ "N": 1, "from": "A", "to": "B", "aux": "C" }
Output:
[ "move disk 1 from rod A to rod B" ]
Loading...

Sign in to Run Code and Submit