20S-Section-Solution

20S-Section-Solution - CS106X Summer 2008 Handout 20S January 30 February 1 2008 Section Solution Problem 1 Towers Of Hanoi Revisited Version 1

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
CS106X Handout 20S Summer 2008 January 30 – February 1, 2008 Section Solution Problem 1: Towers Of Hanoi Revisited Version 1: MoveTower(int numDisks, string start, string dest, string temp) { if (numDisks > 0) { MoveTower(numDisks - 1, start, temp, dest); MoveDisk(start, dest); MoveTower(numDisks - 1, temp, dest, start); } } Correct Incorrect Simply imagine the white disks as being the slightest bit smaller than their dark grey cousins. Then we have the same type of tower as we dealt with in class (to see this, forget about the fact that the disks are shaded different colors.) Version 2: MoveTower(int numDisks, string start, string dest, string temp) { if (numDisks > 0) { MoveTower(numDisks - 2, start, temp, dest); MoveDisk(start, dest); MoveDisk(start, dest); MoveTower(numDisks - 2, temp, dest, start); } } Correct Incorrect This will not work, as it should be clear that the largest white disk will be on bottom after the two calls to MoveDisk are complete. However, the even number of recursive calls will ensure that all pairs except the bottom pair will maintain their original order. Version 3: MoveTower(int numDisks, string start, string dest, string temp) { if (numDisks > 0) { MoveTower(numDisks - 2, start, dest, temp); MoveDisk(start, temp); MoveDisk(start, temp); MoveTower(numDisks - 2, dest, start, temp); MoveDisk(temp, dest); MoveDisk(temp, dest); MoveTower(numDisks - 2, start, dest, temp); } } Correct Incorrect Again, make that leap, and
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/18/2008 for the course CS 106X taught by Professor Cain,g during the Winter '08 term at Stanford.

Page1 / 4

20S-Section-Solution - CS106X Summer 2008 Handout 20S January 30 February 1 2008 Section Solution Problem 1 Towers Of Hanoi Revisited Version 1

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online