2

I am trying to solve the Towers of Hanoi problem using recursion. I understand that if all the rings are on one tower at the start, there's a nice algorithm for solving the problem based on looking at the binary representations of each step in the sequence.

But let's suppose that we want to solve the Towers of Hanoi problem with the rings disorganized at the start. Let Ri denote the ring of radius i. Suppose that initially R5 and R2 are on Pole A, R4 is on Pole B, and R3 and R1 are on Pole C, as shown here:

 **           *
***** ****   ***
  A     B     C

What is the first move if the goal is to move all the rings to Pole B? And, more generally, how would you solve this variant of Towers of Hanoi?

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
vigilant
  • 41
  • 8
  • I would vote to close this a s a duplicate, but hy close-duplicate vote is too powerful. https://stackoverflow.com/questions/49220476/tower-of-hanoi-solving-halfway-algorithm-in-python/49221643#49221643 – Matt Timmermans Sep 09 '20 at 22:27

1 Answers1

2

Fun problem! We can use a recursive insight similar to the one for solving the regular Towers of Hanoi puzzle to solve this problem.

Let's number the disks 1, 2, 3, 4, ..., n by size. Now, suppose we want to end with each disk on spindle B. Look at where disk n is. If disk n is on spindle B, then we never need to move it - it never has any impact on the movement of the other disks, since its placement never bars any movement. At that point, we just need to (recursively) move the other n-1 disks onto spindle B and can basically ignore disk n.

On the other hand, if disk n is on a different spindle - say, spindle A or spindle C - then we will need to move it to spindle B. But the only way that can happen is if all the other disks aren't on top of disk n (then disk n couldn't move) or on top of spindle B (then disk n couldn't move there). This means that we get the following basic setup:

move all disks of size n or less to spindle X:
    # Base case: If we need to move zero disks, there's nothing to do.
    if n == 0: return

    # Recursive case 1: If disk n is already on spindle X, we don't need to
    # do anything fancy! Just move the other disks.
    if disk n is on spindle X:
        recursively move all disks of size n-1 to spindle X
        return

    # Recursive case 2: If disk n isn't on spindle X, it's on some other
    # spindle Y. That means all other disks need to get to the third 
    # spindle Z before we can move disk n.
    recursively move all disks of size n-1 to spindle Z, as defined above.
    move disk n to spindle X.

    # Now, move all the remaining disks back on top of disk n.
    recursively move all disks of size n-1 to spindle X.

The nice part about this solution is that every step is basically forced - there's no decisions to make about what to do and no shortcuts to take. Therefore, this is guaranteed to find the fastest possible way of moving the disks.

Moreover, this solution nicely generalizes the standard recursive algorithm for Towers of Hanoi. Notice that, if all the disks start in the standard configuration, then Recursive Case 1 never triggers and we're left with exactly the same algorithm as before.

Dharman
  • 30,962
  • 25
  • 85
  • 135
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065