1 / 46

ecs30 Winter 2012: Programming and Problem Solving #16: Chapters 9~10, Strings to Recurrsion

ecs30 Winter 2012: Programming and Problem Solving #16: Chapters 9~10, Strings to Recurrsion. Prof . S. Felix Wu Computer Science Department University of California, Davis http://dsl.ucdavis.edu/~wu/ecs30/. HW#6.1.

jackie
Télécharger la présentation

ecs30 Winter 2012: Programming and Problem Solving #16: Chapters 9~10, Strings to Recurrsion

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ecs30 Winter 2012:Programming and Problem Solving#16: Chapters 9~10, Strings to Recurrsion Prof . S. Felix Wu Computer Science Department University of California, Davis http://dsl.ucdavis.edu/~wu/ecs30/ ecs30 Winter 2012 Lecture #17

  2. HW#6.1 • Game of Life – please read the wikipedia page to understand the game itself • A partial/unfinished program for you to complete/finish: • Learn to understand code/program by others • Learn how to complete or modify other’s code. • Reference executables (arch – 64/32 bits) • Executables without source code (reverse engineering) ecs30 Winter 2012 Lecture #17

  3. FILE *out_file_p; FILE *out_debug_file_p; intmain(void) {… /* initialization */ out_file_p= fopen("output.hw6_1", "w"); out_debug_file_ p = fopen("debug.hw6_1", "w"); If (out_file_p == NULL) {exit(-1);} /* using the FILE pointers */ /* regular output example */ fprintf(out_file_p, "...", ...); /* debug output example */ #ifdef ECS30_DEBUG fprintf(out_debug_file_p, "...", ...); #endif /*ECS30_DEBUG */ /* finishing/edning */ fclose(out_file_p); fclose(out_debug_file_p); ecs30 Winter 2012 Lecture #17

  4. Recursion • A function calls itself, maybe with a different set of parameter values… • Different sets of local variables… (n-1)! N! ecs30 Winter 2012 Lecture #17

  5. (m * n) ~ (m + m * (n - 1)) ecs30 Winter 2012 Lecture #17

  6. ecs30 Winter 2012 Lecture #17

  7. It gotta stop… at some point!! Exit Strategy/Condition! ecs30 Winter 2012 Lecture #17

  8. ecs30 Winter 2012 Lecture #17

  9. ecs30 Winter 2012 Lecture #17

  10. The Towers of Hanoi http://en.wikipedia.org/wiki/Tower_of_Hanoi ecs30 Winter 2012 Lecture #17

  11. A Legend Legend has it that there were three diamond needles set into the floor of the temple of Brahma in Hanoi. Stacked upon the leftmost needle were 64 golden disks, each a different size, stacked in concentric order: ecs30 Winter 2012 Lecture #17

  12. A Legend The priests were to transfer the disks from the first needle to the second needle, using the third as necessary. But they could only moveone disk at a time, and could never put a larger disk on top of a smaller one. When they completed this task, the world would end! ecs30 Winter 2012 Lecture #17

  13. To Illustrate For simplicity, suppose there were just 3 disks, and we’ll refer to the three needles as A, B, and C... Since we can only move one disk at a time, we move the top disk from A to B. ecs30 Winter 2012 Lecture #17

  14. Example For simplicity, suppose there were just 3 disks, and we’ll refer to the three needles as A, B, and C... We then move the top disk from A to C. ecs30 Winter 2012 Lecture #17

  15. Example For simplicity, suppose there were just 3 disks, and we’ll refer to the three needles as A, B, and C... We then move the top disk from B to C. ecs30 Winter 2012 Lecture #17

  16. Example For simplicity, suppose there were just 3 disks, and we’ll refer to the three needles as A, B, and C... We then move the top disk from A to B. ecs30 Winter 2012 Lecture #17

  17. Example For simplicity, suppose there were just 3 disks, and we’ll refer to the three needles as A, B, and C... We then move the top disk from C to A. ecs30 Winter 2012 Lecture #17

  18. Example For simplicity, suppose there were just 3 disks, and we’ll refer to the three needles as A, B, and C... We then move the top disk from C to B. ecs30 Winter 2012 Lecture #17

  19. Example For simplicity, suppose there were just 3 disks, and we’ll refer to the three needles as A, B, and C... We then move the top disk from A to B. ecs30 Winter 2012 Lecture #17

  20. Example For simplicity, suppose there were just 3 disks, and we’ll refer to the three needles as A, B, and C... and we’re done! The problem gets more difficult as the number of disks increases... ecs30 Winter 2012 Lecture #17

  21. Our Problem Today’s problem is to write a program that generates the instructions for the priests to follow in moving the disks. While quite difficult to solve iteratively, this problem has a simple and elegant recursive solution. ecs30 Winter 2012 Lecture #17

  22. Testing The Hanoi Towers Enter how many disks: 2 move a disk from A to C move a disk from A to B move a disk from C to B ecs30 Winter 2012 Lecture #17

  23. Testing The Hanoi Towers Enter how many disks: 3 move a disk from A to B move a disk from A to C move a disk from B to C move a disk from A to B move a disk from C to A move a disk from C to B move a disk from A to B ecs30 Winter 2012 Lecture #17

  24. Testing The Hanoi Towers Enter how many disks: 4 move a disk from A to B move a disk from C to B move a disk from A to C move a disk from B to A move a disk from B to C move a disk from A to C move a disk from A to B move a disk from C to B move a disk from C to A move a disk from B to A move a disk from C to B move a disk from A to C move a disk from A to B move a disk from C to B ecs30 Winter 2012 Lecture #17

  25. Analysis The tower function: void tower (char from_peg, char to_peg, char aux_peg, int n) { … } ecs30 Winter 2012 Lecture #17

  26. void tower (char from_peg, char to_peg, char aux_peg, int n) { … } int main(void) { int n; scanf(“%d”, &n); tower(‘A’, ‘B’, ‘C’, n); } ecs30 Winter 2012 Lecture #17

  27. Testing The Hanoi Towers Enter how many disks: 2 move a disk from A to C move a disk from A to B move a disk from C to B ecs30 Winter 2012 Lecture #17

  28. Testing The Hanoi Towers Enter how many disks: 3 move a disk from A to B move a disk from A to C move a disk from B to C move a disk from A to B move a disk from C to A move a disk from C to B move a disk from A to B ecs30 Winter 2012 Lecture #17

  29. Testing The Hanoi Towers Enter how many disks: 4 move a disk from A to B move a disk from C to B move a disk from A to C move a disk from B to A move a disk from B to C move a disk from A to C move a disk from A to B move a disk from C to B move a disk from C to A move a disk from B to A move a disk from C to B move a disk from A to C move a disk from A to B move a disk from C to B ecs30 Winter 2012 Lecture #17

  30. The Tower FunctionFigure 10.24 void tower (char from_peg, char to_peg, char aux_peg, int n) { … } ecs30 Winter 2012 Lecture #17

  31. Design: “Exit Condition” Basis: What is an instance of the problem that is trivial? ® n == 1 Since this base case could occur when the disk is on any needle, we simply output the instruction to move the top disk from src to dest. ecs30 Winter 2012 Lecture #17

  32. Design Basis: What is an instance of the problem that is trivial? ® n == 1 Since this base case could occur when the disk is on any needle, we simply output the instruction to move the top disk from src to dest. ecs30 Winter 2012 Lecture #17

  33. The Tower FunctionFigure 10.24 void tower (char from_peg, char to_peg, char aux_peg, int n) { if (n == 1) printf(“move a disk from %c to %c\n”, from_peg, to peg); … } ecs30 Winter 2012 Lecture #17

  34. Design (Ct’d) Induction Step: n > 1 ® How can recursion help us out? a. Recursively move n-1 disks from src to aux. ecs30 Winter 2012 Lecture #17

  35. Design (Ct’d) Induction Step: n > 1 ® How can recursion help us out? b. Move the one remaining disk from src to dest. ecs30 Winter 2012 Lecture #17

  36. Design (Ct’d) Induction Step: n > 1 ® How can recursion help us out? c. Recursively move n-1 disks from aux to dest... ecs30 Winter 2012 Lecture #17

  37. Design (Ct’d) Induction Step: n > 1 ® How can recursion help us out? d. We’re done! ecs30 Winter 2012 Lecture #17

  38. The Tower FunctionFigure 10.24 void tower (char from_peg, char to_peg, char aux_peg, int n) { if (n == 1) printf(“move a disk from %c to %c\n”, from_peg, to peg); … } ecs30 Winter 2012 Lecture #17

  39. The Tower FunctionFigure 10.24 void tower (char from_peg, char to_peg, char aux_peg, int n) { if (n == 1) printf(“move a disk from %c to %c\n”, from_peg, to peg); tower(from_peg, aux_peg, to_peg, n-1); tower(from_peg, to_peg, aux_peg, 1); tower(aux_peg, to_peg, from_peg, n-1); } ecs30 Winter 2012 Lecture #17

  40. The Tower FunctionFigure 10.24 void tower (char from_peg, char to_peg, char aux_peg, int n) { if (n == 1) printf(“move a disk from %c to %c\n”, from_peg, to peg); tower(from_peg, aux_peg, to_peg, n-1); tower(from_peg, to_peg, aux_peg, 1); tower(aux_peg, to_peg, from_peg, n-1); } ecs30 Winter 2012 Lecture #17

  41. The Tower FunctionFigure 10.24 void tower (char from_peg, char to_peg, char aux_peg, int n) { if (n == 1) printf(“move a disk from %c to %c\n”, from_peg, to peg); tower(from_peg, aux_peg, to_peg, n-1); tower(from_peg, to_peg, aux_peg, 1); tower(aux_peg, to_peg, from_peg, n-1); } ecs30 Winter 2012 Lecture #17

  42. The Tower FunctionFigure 10.24 void tower (char from_peg, char to_peg, char aux_peg, int n) { if (n == 1) printf(“move disk %d from %c to %c\n”, n, from_peg, to peg); tower(from_peg, aux_peg, to_peg, n-1); tower(from_peg, to_peg, aux_peg, 1); tower(aux_peg, to_peg, from_peg, n-1); } ecs30 Winter 2012 Lecture #17

  43. Testing The Hanoi Towers Enter how many disks: 2 move disk 1 from A to C move disk 2 from A to B move disk 1 from C to B ecs30 Winter 2012 Lecture #17

  44. HW#7 • Tower of Hanoi with FOUR Pegs ecs30 Winter 2012 Lecture #17

  45. HW#7 • Tower of Hanoi with FOUR Pegs Number of Moves? (needs to be smaller than 3 pegs) ecs30 Winter 2012 Lecture #17

  46. http://www.exocortex.org/toh/ void tower4P (char from_peg, char to_peg, char aux1_peg, char aux2_peg, int n) { if (n == 1) printf(“move disk %d from %c to %c\n”, n, from_peg, to peg); … } ecs30 Winter 2012 Lecture #17

More Related