The Towers of Hanoi is a well-known mathematical puzzle that introduces core ideas of algorithmic problem-solving. It consists of three rods and a number of disks of different sizes which can slide onto any rod. The puzzle starts with the disks neatly stacked in ascending order on one rod, with the smallest at the top.
The objective is to move the entire stack to another rod, following two rules:
- Only one disk can be moved at a time.
- A larger disk cannot be placed on top of a smaller disk.
The problem is typically approached using recursion, a method where the solution involves solving smaller instances of the same problem. The recursive algorithm follows these steps:
- Move the top \(n-1\) disks from the source rod to an auxiliary rod.
- Move the \(n\)-th disk directly to the target rod.
- Move the \(n-1\) disks from the auxiliary rod to the target rod on top of the \(n\)-th disk.
With each additional disk, the complexity increases exponentially, illustrating why this problem fits into the category of NP-hard problems.