Saturday, June 14, 2008

Towers of Hanoi problem

Procedure move (n, X, Z, Y).

The above Procedure moves ‘n’ disks from peg X to peg Z using temporary peg Y. In this, ‘n’ denotes the number of disk in peg X. Here move function is defined in a recursive manner.


Step 1 n = 1, one disk to move

if (n = 1) then

call to move (n, X, Z, Y)


Step 2 if n is greater than one

call to move (n – 1, X, Y, Z)

moving (n – 1) disks from peg X to peg Y, using peg Z

call to move (n – 1, Y, Z, X)

moving (n – 1) disk from peg Y to peg Z, using peg X.


Step 3 return at the point of call

return.

No comments: