HELP! Need help with this algorithm 
Author Message
 HELP! Need help with this algorithm

    I have to write code to document the steps to the puzzle of the
Tower of Hanoi.  Basically, in this puzzle there are three poles and
rings of increasing size on the 1st pole.  The object is to get the
rings to the 2nd
 pole in increasing size, but you can only move one piece at a time,
and a bigger piece can't be on a smaller one.  Example:


Pole 1     Pole 2    Pole 3


Pole 1     Pole 2    Pole 3

You have to use the conditions above

Here is my algorithm:

The disks will always move from 1 to 3 and then back to 1.  But the nth
disk can't change direction to come back until the n+1 disk moves 1
spot.  Example:  3 variable disk1, disk2, disk3 can have value of 0, 1,
or 2 for
the poles 1, 2, and 3 respectively.  Here is the steps for 3 disks.

Disk1, 2, 3 = 0 and you want to end up with Disk 1, 2, 3 = 1
Disk1 =1
Disk 1 =2
Disk 2 =1
Disk 1 = 1
Disk 1=0
Disk2 =2
Disk 1 =1
Disk 1 = 2
Disk 3 =1
Disk 1 = 1
Disk 1 = 0
Disk 2 = 1
Disk 1 =1

That will accomplish my task.

My problem is coding the above algorithm, does recursion need to be
used?  Are there some kind of nested for loops with if statements??


Mon, 08 Dec 1997 03:00:00 GMT  
 HELP! Need help with this algorithm


>    I have to write code to document the steps to the puzzle of the
>Tower of Hanoi.


The basic algorithm for towers of Hanoi is:

To move a stack of n disks from pole x to poll y you:
        move a stack of n-1 disks to poll z (the poll that isn't x and isn't y).
        move the nth disk to poll y.
        move the stack of n-1 disks from poll z to poll y.

If n is 1, you can move the disk directly.

This can be coded efficiently using recursion.

Good luck!

AT&T Bell Laboratories    (908)946-1107
943 Holmdel Road
Holmdel, NJ 07733

Tue, 09 Dec 1997 03:00:00 GMT  
 [ 2 post ] 

 Relevant Pages 

1. inductive algorithm for permutations - help needed

2. Help! power/modulo algorithm needed

3. We need some help to implement Strassen algorithm in AIX

4. Help! power/modulo algorithm needed

5. Help: need Mutual exclusion algorithm and code

6. help needed finding algorithms

7. Need Help: seek an algorithm computing a 3D Volume points giving vertexes

8. Newbie in desperate need of help - sort algorithm

9. Recusive algorithm - help needed

10. Recursive algorithm - help needed

11. C Algorithm Help needed

12. Need Help with Algorithm


Powered by phpBB® Forum Software