{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

20S-Section-Solution

# 20S-Section-Solution - CS106X Summer 2008 Handout 20S...

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

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

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 4

20S-Section-Solution - CS106X Summer 2008 Handout 20S...

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

View Full Document
Ask a homework question - tutors are online