-1

Here is the Python code I've written for the Tower of Hanoi Problem where the tower must be transferred from the left peg to the middle peg, using the right peg as the spare:

def hanoi(n, origin = "1", destination = "2", spare = "3"):

    if n == 1:
        print ("Move disk from", origin, "to", destination)

    else:
        hanoi(n-1, origin, spare, destination)
        hanoi(1, origin, destination, spare)
        hanoi(n-1, spare, destination, origin)

hanoi(3)

Now my professor wants me to implement it in a way where the legal moves are only from tower1 to 2, tower2 to 3, and tower3 to 1. All other rules are the same.

phant0m
  • 16,595
  • 5
  • 50
  • 82

1 Answers1

4

As always with this problem, you need to think inductively. Start with the smallest possible tower to move, then ask yourself: If I can do that, how can I move a larger tower?

Since moving a tower of size one is trivial, let's start with a tower of size 2:

Base case

Moving a tower of size two one peg to the right:

|  -  |     |     |
| --- |     |     |
-------------------
Step 1:

|     |     |     |   
| --- |  -  |     |
-------------------
Step 2:

|     |     |     |
| --- |     |  -  |
-------------------
Step 3:

|     |     |     |
|     | --- |  -  |
-------------------
Step 4:

|     |     |     |
|  -  | --- |     |
-------------------
Step 5:

|     |  -  |     |
|     | --- |     |
-------------------

This demonstrates how you can move the tower one peg to the right. Of course, this can also be used to move the tower from the second to the third, or from the third to the first peg.

Step

Let's say you know how to move a tower of size n one peg to the right, here's how you do it for n + 1 disks:

|    -    |         |         | Move the small tower one peg to the right
|   ---   |         |         |
|  -----  |         |         |
| ------- |         |         |
-------------------------------
Step 1:

|         |         |         | Move it another step to the right
|         |    -    |         |
|         |   ---   |         |
| ------- |  -----  |         |
-------------------------------
Step 2:

|         |         |         | Let's move the n+1 disk one peg to the right
|         |         |    -    |
|         |         |   ---   |
| ------- |         |  -----  |
-------------------------------
Step 3:

|         |         |         | Move the small tower to the right
|         |         |    -    |
|         |         |   ---   |
|         | ------- |  -----  |
-------------------------------
Step 4: 

|         |         |         | Move the small tower another peg to the right
|    -    |         |         |
|   ---   |         |         |
|  -----  | ------- |         |
-------------------------------
Final step:

|         |    -    |         | 
|         |   ---   |         |
|         |  -----  |         |
|         | ------- |         |
-------------------------------
TemporalWolf
  • 7,727
  • 1
  • 30
  • 50
phant0m
  • 16,595
  • 5
  • 50
  • 82