What if a simple rule could solve a puzzle that seems impossible to plan by hand?
Why Tower of Hanoi Problem in DSA C?
Imagine you have three pegs and a stack of different sized disks on one peg. You want to move all disks to another peg, but you can only move one disk at a time and never place a larger disk on top of a smaller one.
Trying to figure out the steps manually for many disks is confusing and easy to mess up.
Manually planning each move for many disks is slow and error-prone. You might forget a step or break the rules by placing a bigger disk on a smaller one.
It becomes very hard to keep track of all moves as the number of disks grows.
The Tower of Hanoi problem uses a simple set of rules and recursion to break the big problem into smaller ones. This method gives a clear, step-by-step solution that always works, no matter how many disks.
It saves time and avoids mistakes by following a repeatable pattern.
/* Manually list moves for 3 disks */ move disk 1 from A to C move disk 2 from A to B move disk 1 from C to B move disk 3 from A to C move disk 1 from B to A move disk 2 from B to C move disk 1 from A to C
void towerOfHanoi(int n, char from, char to, char aux) { if (n == 1) { printf("Move disk 1 from %c to %c\n", from, to); return; } towerOfHanoi(n-1, from, aux, to); printf("Move disk %d from %c to %c\n", n, from, to); towerOfHanoi(n-1, aux, to, from); }
This problem teaches how to solve complex tasks by breaking them into smaller, repeatable steps using recursion.
Moving boxes between shelves with size limits or organizing tasks where some must be done before others can be solved using the same step-by-step approach.
Manual planning is hard and error-prone for many disks.
Recursion breaks the problem into smaller, manageable parts.
The Tower of Hanoi solution is a clear, repeatable method for moving disks correctly.
Practice
Solution
Step 1: Understand the formula for minimum moves
The minimum moves required for n disks is 2^n - 1.Step 2: Calculate for 3 disks
For 3 disks, minimum moves = 2^3 - 1 = 8 - 1 = 7.Final Answer:
7 -> Option DQuick Check:
2^3 - 1 = 7 [OK]
- Using n instead of 2^n - 1
- Calculating 2*n - 1 instead of 2^n - 1
- Confusing number of disks with moves
Solution
Step 1: Identify parameters needed for Tower of Hanoi
We need number of disks and names of three rods: source, destination, auxiliary.Step 2: Check function signature matching these parameters
void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod); has int n and three char rods, matching the problem requirements.Final Answer:
void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod); -> Option AQuick Check:
Function needs 3 rods and disk count [OK]
- Missing auxiliary rod parameter
- Using int instead of char for rods
- Returning int instead of void
void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod) {
if (n == 1) {
printf("Move disk 1 from rod %c to rod %c\n", from_rod, to_rod);
return;
}
towerOfHanoi(n-1, from_rod, aux_rod, to_rod);
printf("Move disk %d from rod %c to rod %c\n", n, from_rod, to_rod);
towerOfHanoi(n-1, aux_rod, to_rod, from_rod);
}
int main() {
towerOfHanoi(2, 'A', 'C', 'B');
return 0;
}Solution
Step 1: Trace recursive calls for n=2
First move disk 1 from A to B, then disk 2 from A to C, then disk 1 from B to C.Step 2: Match output with options
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 matches the exact sequence of moves printed by the code.Final Answer:
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 -> Option CQuick Check:
Recursive moves for 2 disks = 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 [OK]
- Swapping auxiliary and destination rods
- Printing moves in wrong order
- Confusing disk numbers
void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod) {
if (n == 0) {
return;
}
towerOfHanoi(n-1, from_rod, aux_rod, to_rod);
printf("Move disk %d from rod %c to rod %c\n", n, from_rod, to_rod);
towerOfHanoi(n-1, aux_rod, to_rod, from_rod);
}Solution
Step 1: Analyze base case condition
Base case should stop recursion when n == 1, not 0, to print the last move.Step 2: Understand effect of current base case
With n == 0, no moves are printed for the smallest disk, causing no output.Final Answer:
Change base case condition to if (n == 1) instead of if (n == 0) -> Option AQuick Check:
Base case must be n == 1 to print moves [OK]
- Using n == 0 as base case
- Changing print statement unnecessarily
- Swapping rods incorrectly
void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod) {
if (n == 1) {
printf("Move disk 1 from rod %c to rod %c\n", from_rod, to_rod);
return;
}
towerOfHanoi(n-1, from_rod, aux_rod, to_rod);
printf("Move disk %d from rod %c to rod %c\n", n, from_rod, to_rod);
towerOfHanoi(n-1, aux_rod, to_rod, from_rod);
}Solution
Step 1: Understand requirement to print only disk 1 moves
We must print moves only when disk number is 1 inside the recursive function.Step 2: Modify print statements accordingly
Keep recursive calls intact but print only when n == 1, skipping other prints.Final Answer:
Add condition to print only if n == 1 inside the function -> Option BQuick Check:
Print condition inside recursion for disk 1 only [OK]
- Removing recursive calls breaks solution
- Filtering outside loses recursion context
- Changing base case incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
