CT042

Tower of Hanoi

EasyAcceptance: 0.0%

The Tower of Hanoi is a puzzle with three rods and n disks of different sizes. All disks start on the first rod, ordered by size (largest at bottom). Move all disks to the last rod following these rules: move one disk at a time, only the topmost disk can be moved, and a larger disk cannot be placed on a smaller disk. Given n, print all moves to solve the puzzle. Rods are numbered 1, 2, 3. Output each move as "from to" on a separate line.

Example 1:

Input: 3
Output: 1 3 1 2 3 2 1 3 2 1 2 3 1 3

Example 2:

Input: 1
Output: 1 3

Constraints:

1 <= n <= 20

Tags:

recursion divide-and-conquer
Loading...
Test Cases:No test cases
No test cases available.