next next up down toc toc mail

8.7.  How to move n rings from peg A to peg C?

If you know how to move n-1 rings from one peg to another then simply move n-1 rings to the spare peg -- there is only one ring left on the source peg now, simply move it to the destination, then pile the rest of them from the spare peg onto the destination peg.

For example when n is 4 and A B and C ...

More generally ...

move   n-1 rings from A to B
move   one ring from A to C
move   n-1 rings from B to C

As with most recursive solutions we have to treat some base case specially -- here the base case occurs where we have only one ring to move.

The algorithm as a Nassi Shneidermann diagram:

Procedure move(int n_rings, peg from, peg over, peg to):

[picture]

The C-Source code:

 1      /*      Module:         3_hanoi.c
 2       *      Description:    Solution for the tower of Hanoi Problem
 3       */
 4      
 5      #include <stdio.h>
 6      
 7      /* number to move, source pole,destination pole and
 8       * spare pole respectively
 9       */
10      void move(int n_rings, char from, char over, char to)
11      {
12          printf("My job: Move %d rings from %c to %c.\n",
13                      n_rings, from, to);
14          if (n_rings==1)
15              printf("\tI move from %c to %c.\n", from, to);
16          else {
17              move(n_rings-1, from, to, over);
18              move(1, from, over, to);
19              move(n_rings-1, over, from, to);
20          }
21      }
22      
23      int main(void)
24      {
25      int n_rings;
26          printf("Rings: ");
27          scanf("%d", &n_rings);
28          if ( n_rings > 0 )  {
29              printf("Tower of Hanoi with %d rings.\n", n_rings);
30              /*            from  over    to  */
31              move(n_rings, 'A',  'B',    'C');
32          } else
33              printf("TOH with %d rings make no sense.\n", n_rings);
34          exit(0);
35      }

An example:

% hanoi
Rings: 3
Tower of Hanoi with 3 rings.
My job: Move 3 rings from A to C.
My job: Move 2 rings from A to B.
My job: Move 1 rings from A to C.
        I move from A to C.
My job: Move 1 rings from A to B.
        I move from A to B.
My job: Move 1 rings from C to B.
        I move from C to B.
My job: Move 1 rings from A to C.
        I move from A to C.
My job: Move 2 rings from B to C.
My job: Move 1 rings from B to A.
        I move from B to A.
My job: Move 1 rings from B to C.
        I move from B to C.
My job: Move 1 rings from A to C.
        I move from A to C.

Tower of Hanoi with 4 rings.
Move from A to C.
Move from A to B.
Move from C to B.
Move from A to C.
Move from B to A.
Move from B to C.
Move from A to C.
Move from A to B.
Move from C to B.
Move from C to A.
Move from B to A.
Move from C to B.
Move from A to C.
Move from A to B.
Move from C to B.


back next up down toc toc mail


Created by unroff & hp-tools. © by Hans-Peter Bischof. All Rights Reserved (1998).

Last modified: 27/July/98 (12:14)