Tower of Hanoi Modified

Tower of Hanoi Modified – When the First Rod is a No-Go

Problem Statement

You’re given a classic Tower of Hanoi setup with three rods (A, B, and C) and N disks of different sizes stacked on Rod A, smallest on top.

BUT there’s a twist...

You cannot move a disk directly between rod A and rod C.

You must always pass through rod B when going from A ➡️ C or C ➡️ A.

 Goal: Move all disks from Rod A to Rod C, following the usual rules plus this new restriction, and print:

  1. The minimum number of moves required.

  2. The actual sequence of moves (formatted like Move x from A to B).

Example

Input:

2 1 2

Output:

2 Move 1 from A to B Move 1 from B to C 8 Move 1 from A to B Move 1 from B to C Move 2 from A to B Move 1 from C to B Move 1 from B to A Move 2 from B to C Move 1 from A to B Move 1 from B to C

Best Data Structure for the Job

No need for trees, heaps, or graphs here. Just:

  • Simple recursion 

  • Strings to log each move

Different Approaches

Approach: Modified Recursion (with No Direct A <-> C)

Let’s break it down:

  • In classic Hanoi, the recursive strategy is:

    1. Move n-1 disks from source ➡️ aux

    2. Move 1 disk from source ➡️ target

    3. Move n-1 from aux ➡️ target

In this version, if the move is A ➡️ C or C ➡️ A, you’re forced to use B in between, so you need 2 moves instead of 1.

Recursive Logic:

import java.util.*; public class ModifiedHanoi { static void hanoiModified(int n, char source, char target, char auxiliary, List<String> moves) { if (n == 0) return; // If direct move between A <-> C, go through B if ((source == 'A' && target == 'C') || (source == 'C' && target == 'A')) { hanoiModified(n, source, auxiliary, target, moves); hanoiModified(n, auxiliary, target, source, moves); } else { hanoiModified(n - 1, source, auxiliary, target, moves); moves.add("Move " + n + " from " + source + " to " + target); hanoiModified(n - 1, auxiliary, target, source, moves); } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); // number of test cases while (T-- > 0) { int N = sc.nextInt(); // number of disks List<String> moves = new ArrayList<>(); hanoiModified(N, 'A', 'C', 'B', moves); System.out.println(moves.size()); for (String move : moves) { System.out.println(move); } } sc.close(); } }

⚠️ Common Mistakes and Pitfalls

  • Forgetting the A <-> C restriction—this is where classic logic will break.

  • Not maintaining the exact rod names: Stick to 'A', 'B', 'C' for output.

  • Infinite recursion if base case (n==0) isn’t handled.

📊 Time and Space Complexity Analysis

Approach      Time ComplexitySpace Complexity
Modified Hanoi        O(3ⁿ)        O(3ⁿ) for call stack and moves

🌍 Real-world Applications

  • Cognitive Skill Training: Used in psych tests to assess planning.

  • Automated Storage/Retrieval: Systems where direct paths are restricted.

  • Game Level Design: Puzzles with movement constraints.

  • Robotics: Moving objects with limited access points.    


I hope you found this article insightful! Keep exploring, keep learning, and don’t forget to check back daily for more exciting problem breakdowns. If you know someone who would benefit from this, share it with them and help them grow too! That’s it for today—see you in the next post!

                                                                                                                                         Signing off!! 

                                                                                                Master DSA One Problem at a Time :)

Comments

Popular posts from this blog

Trailing Zeros in Factorial – How Many and Why?

Divisible Drama — Who’s In, Who’s Out?

Zero Array After Queries