All about flooble | fun stuff | Get a free chatterbox | Free JavaScript | Avatars    
perplexus dot info

Home > Algorithms
Towers of Hanoi (Posted on 2003-09-07) Difficulty: 3 of 5
You have three small poles and five hoops - XS, S, M, L, XL (as in extra small, small, medium, large and extra large). They are placed on pole 1 in order, with largest at the bottom.

You can move one hoop at a time, and the hoops you are not moving have to be on a pole. You also cannot place a hoop on top of a smaller one. How can you move the hoops so that they are in the same order as they are now, but on pole 3?

See The Solution Submitted by Lewis    
Rating: 3.0667 (15 votes)

Comments: ( Back to comment list | You must be logged in to post comments.)
re: Stacks--Basic version | Comment 18 of 22 |
(In reply to Stacks by DJ)

DECLARE SUB display ()
DECLARE SUB transfer (n!, source!, dest!)
DIM SHARED pole$(3), n
INPUT "Number of hoops:", n
FOR i = 1 TO n
  pole$(1) = CHR$(64 + i) + pole$(1)
NEXT
transfer n, 1, 3

SUB display
  FOR i = n TO 1 STEP -1
    FOR p = 1 TO 3
      IF LEN(pole$(p)) < i THEN
        PRINT \" | \";
      ELSE
        PRINT \" \"; MID$(pole$(p), i, 1); \" \";
      END IF
    NEXT
    PRINT
  NEXT
END SUB

SUB transfer (n, source, dest)
  IF n = 1 THEN
    PRINT \"Move A from\"; source; \"to\"; dest
    pole$(dest) = pole$(dest) + RIGHT$(pole$(source), 1)
    pole$(source) = LEFT$(pole$(source), LEN(pole$(source)) - 1)
    display
  ELSE
    transfer n - 1, source, 6 - source - dest
    PRINT \"Move \"; CHR$(ASC(\"A\") + n - 1); \" from\"; source; \"to\"; dest
    pole$(dest) = pole$(dest) + RIGHT$(pole$(source), 1)
    pole$(source) = LEFT$(pole$(source), LEN(pole$(source)) - 1)
    display
    transfer n - 1, 6 - source - dest, dest
  END IF
END SUB

With results:
Number of hoops:4
Move A from 1 to 2
| | |
B | |
C | |
D A |
Move B from 1 to 3
| | |
| | |
C | |
D A B
Move A from 2 to 3
| | |
| | |
C | A
D | B
Move C from 1 to 2
| | |
| | |
| | A
D C B
Move A from 3 to 1
| | |
| | |
A | |
D C B
Move B from 3 to 2
| | |
| | |
A B |
D C |
Move A from 1 to 2
| | |
| A |
| B |
D C |
Move D from 1 to 3
| | |
| A |
| B |
| C D
Move A from 2 to 3
| | |
| | |
| B A
| C D
Move B from 2 to 1
| | |
| | |
| | A
B C D
Move A from 3 to 1
| | |
| | |
A | |
B C D
Move C from 2 to 3
| | |
| | |
A | C
B | D
Move A from 1 to 2
| | |
| | |
| | C
B A D
Move B from 1 to 3
| | |
| | B
| | C
| A D
Move A from 2 to 3
| | A
| | B
| | C
| | D

Number of hoops:5
Move A from 1 to 3
| | |
B | |
C | |
D | |
E | A
Move B from 1 to 2
| | |
| | |
C | |
D | |
E B A
Move A from 3 to 2
| | |
| | |
C | |
D A |
E B |
Move C from 1 to 3
| | |
| | |
| | |
D A |
E B C
Move A from 2 to 1
| | |
| | |
A | |
D | |
E B C
Move B from 2 to 3
| | |
| | |
A | |
D | B
E | C
Move A from 1 to 3
| | |
| | |
| | A
D | B
E | C
Move D from 1 to 2
| | |
| | |
| | A
| | B
E D C
Move A from 3 to 2
| | |
| | |
| | |
| A B
E D C
Move B from 3 to 1
| | |
| | |
| | |
B A |
E D C
Move A from 2 to 1
| | |
| | |
A | |
B | |
E D C
Move C from 3 to 2
| | |
| | |
A | |
B C |
E D |
Move A from 1 to 3
| | |
| | |
| | |
B C |
E D A
Move B from 1 to 2
| | |
| | |
| B |
| C |
E D A
Move A from 3 to 2
| | |
| A |
| B |
| C |
E D |
Move E from 1 to 3
| | |
| A |
| B |
| C |
| D E
Move A from 2 to 1
| | |
| | |
| B |
| C |
A D E
Move B from 2 to 3
| | |
| | |
| | |
| C B
A D E
Move A from 1 to 3
| | |
| | |
| | A
| C B
| D E
Move C from 2 to 1
| | |
| | |
| | A
| | B
C D E
Move A from 3 to 2
| | |
| | |
| | |
| A B
C D E
Move B from 3 to 1
| | |
| | |
| | |
B A |
C D E
Move A from 2 to 1
| | |
| | |
A | |
B | |
C D E
Move D from 2 to 3
| | |
| | |
A | |
B | D
C | E
Move A from 1 to 3
| | |
| | |
| | A
B | D
C | E
Move B from 1 to 2
| | |
| | |
| | A
| | D
C B E
Move A from 3 to 2
| | |
| | |
| | |
| A D
C B E
Move C from 1 to 3
| | |
| | |
| | C
| A D
| B E
Move A from 2 to 1
| | |
| | |
| | C
| | D
A B E
Move B from 2 to 3
| | |
| | B
| | C
| | D
A | E
Move A from 1 to 3
| | A
| | B
| | C
| | D
| | E

-----------
-----------
Edited on September 9, 2003, 2:13 pm
  Posted by Charlie on 2003-09-09 14:07:51

Please log in:
Login:
Password:
Remember me:
Sign up! | Forgot password


Search:
Search body:
Forums (1)
Newest Problems
Random Problem
FAQ | About This Site
Site Statistics
New Comments (6)
Unsolved Problems
Top Rated Problems
This month's top
Most Commented On

Chatterbox:
Copyright © 2002 - 2024 by Animus Pactum Consulting. All rights reserved. Privacy Information