recursion - Cannot make sense of my lecturers recursive algorithm for the Towers of Hanoi -


the algorithm follows :

algorithm move(k, from, to, spare) if k >0 move(k−1, from, spare, to) printf (”move top disc %d %d\n”, from, to) move(k−1, spare, to, from) 

k number of disks (http://en.wikipedia.org/wiki/tower_of_hanoi). understand recursion, don't understand how meant work, can make sense of this?

sorry being vague in description here, it's understanding of what's happening pretty vague - have no clue printf line doing seems pivotal whole function.

the recursive algorithm broken down 3 steps:

  1. move discs 1 "spare" peg
  2. move last disc destination peg
  3. move discs 1 (those step 1) spare peg destination peg

thus discs have been moved destination peg.

steps 1 , 3 2 recursive move(k-1, ...) calls in pseudocode. step 2 modelled printf.

the point here steps 1 , 3 recurse more calls move, , each call move k > 0 prints 1 instruction line prinft. happen algorithm print steps need take move discs in ultimate detail, 1 one.

in effect, pseudocode not implement algorithm moving pegs; it's algorithm providing instructions human, if will.


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -