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: Recursion challenge | Comment 11 of 22 |
(In reply to Recursion challenge by Gamer)

DECLARE SUB transfer (n!, source!, dest!)
INPUT "Number of hoops:", n
transfer n, 1, 3

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

with sample runs:
Number of hoops:3
Move A from 1 to 3
Move B from 1 to 2
Move A from 3 to 2
Move C from 1 to 3
Move A from 2 to 1
Move B from 2 to 3
Move A from 1 to 3

Number of hoops:4
Move A from 1 to 2
Move B from 1 to 3
Move A from 2 to 3
Move C from 1 to 2
Move A from 3 to 1
Move B from 3 to 2
Move A from 1 to 2
Move D from 1 to 3
Move A from 2 to 3
Move B from 2 to 1
Move A from 3 to 1
Move C from 2 to 3
Move A from 1 to 2
Move B from 1 to 3
Move A from 2 to 3
  Posted by Charlie on 2003-09-07 16:12:32

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


Search:
Search body:
Forums (0)
Newest Problems
Random Problem
FAQ | About This Site
Site Statistics
New Comments (23)
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