lecture 6
play

Lecture 6 Log into Linux Does everyone have the in-class exercise - PowerPoint PPT Presentation

Lecture 6 Log into Linux Does everyone have the in-class exercise from last class (factal.cpp)? Questions? Questions about Homework 3? Monday, September 6 CS 215 Fundamentals of Programming II - Lecture 6 1 Outline Recursive


  1. Lecture 6  Log into Linux  Does everyone have the in-class exercise from last class (factal.cpp)? Questions?  Questions about Homework 3? Monday, September 6 CS 215 Fundamentals of Programming II - Lecture 6 1

  2. Outline  Recursive patterns  Searching a maze  Exhaustive search with backtracking Monday, September 6 CS 215 Fundamentals of Programming II - Lecture 6 2

  3. In-class Exercise, Last Class  Modify RandomFractal so that the movements of the midpoints are no longer random. Instead, at the first level midpoint should be moved up by the width amount, the midpoints at the next level should be moved down by the width amount, the next level up, then down and so on.  Compile, run, and test your program.  When you are done, print out your program and turn it in. Monday, September 6 CS 215 Fundamentals of Programming II - Lecture 6 3

  4. A Question  Suppose a program has the following call: RandomFractal (0, 0, 8, 1);  This original call will generate two recursive calls, and each of those will make two calls, and so forth until the width is less than or equal to epsilon. How many total calls will be made of RandomFractal, including the original call? Monday, September 6 CS 215 Fundamentals of Programming II - Lecture 6 4

  5. Divide and Conquer  RandomFractal is an example of the recursive pattern: divide and conquer  General structure of this pattern  Check for the base case  Divide the problem into two equal-sized problems and make two recursive calls to solve them.  Combine the two results into the result for the original problem. (Note: for RandomFractal, the combination step is simply to execute the calls in left-right order.) Monday, September 6 CS 215 Fundamentals of Programming II - Lecture 6 5

  6. Searching a Maze  A maze is a puzzle in the form of a complex branching passage through which the solver must find a route. For our maze, we will assume that we do not know what the maze looks like, and that we are looking for a secret treasure and want to return to the starting point.  We will assume that our maze is built on a rectangular grid. At each point of the grid, there are four directions to move: north, east, south, or west. Some directions may be blocked. Monday, September 6 CS 215 Fundamentals of Programming II - Lecture 6 6

  7. Problem Specification  Write a function TraverseMaze that can be executed on a portable computer carried through the maze. It should ask questions and give directions to take you to the secret treasure and back out the same way.  The function assumes that the user is facing an unblocked spot in the maze that has not previously been visited by the user. (I.e., it is not a dead end .)  The function returns true if the user has found the secret treasure; otherwise it returns false. Monday, September 6 CS 215 Fundamentals of Programming II - Lecture 6 7

  8. Example Run Step forward & mark the ground Have you found the treasure? No Turn left 90 degrees Are you facing a wall? No Is the ground ahead marked? No Step forward & mark the ground Have you found the treasure? No Turn left 90 degrees Are you facing a wall? Yes Turn right 90 degrees Are you facing a wall? X No Is the ground ahead marked? No Monday, September 6 CS 215 Fundamentals of Programming II - Lecture 6 8

  9. Example Run Step forward & mark the ground Have you found the treasure? Yes Pick up the treasure and step backward Turn right 90 degrees Turn right 90 degrees Step forward, then turn 180 degrees Turn right 90 degrees Turn right 90 degrees Turn right 90 degrees Step forward, then turn 180 degrees X You found it! Monday, September 6 CS 215 Fundamentals of Programming II - Lecture 6 9

  10. Design  Goal: explore maze in this direction; at end of function, user is standing exactly like they were at the beginning with indicator of success  Always have the user step forward into the new spot and mark the ground at the start and return the result of search at the end  Base case:  Condition: the user has found the treasure  Action: pick up treasure and step backwards (the user is still facing the same direction as she started) Monday, September 6 CS 215 Fundamentals of Programming II - Lecture 6 10

  11. Design  Recursive step 1. Have the user turn left 2. For each of the next three directions 2.1 If the treasure isn't found and this direction is not a dead end 2.1.1 Explore this direction, returning to this spot after the exploration, keeping track of whether the treasure was found 2.2 Have the user turn right (to next direction) 3. Have the user step forward and turn around (so that she is in the spot she started at and facing the same direction) Monday, September 6 CS 215 Fundamentals of Programming II - Lecture 6 11

  12. Implementation  Copy files /home/hwang/cs215/lecture06/*.* from csserver. Examine maze.cpp  Helper function:  DeadEnd ( ) - asks user if the way to the next spot is blocked or if the spot has already been visited; returns true if the answer is Yes to either question. bool DeadEnd() { // inquire() is from useful.cpp. It asks the user // a Yes/No question. Returns true for Yes, // false for No // Note the use of short-circuit evaluation return inquire ("Are you facing a wall?") || inquire ("Is the ground ahead marked?"); } Monday, September 6 CS 215 Fundamentals of Programming II - Lecture 6 12

  13. Implementation  TraverseMaze neither receives nor passes back any data, so it has no parameters; it returns a bool bool TraverseMaze ();  Local variables:  found – set to true when treasure has been found;  direction – integer to count the directions.  Step forward and mark the ground cout << "Step forward & marked the ground\n"; Monday, September 6 CS 215 Fundamentals of Programming II - Lecture 6 13

  14. Implementation  Base case: bool found = inquire ("Have you found the treasure?"); if (found) cout << "Pick up the treasure and step backward\n";  Recursive step else { cout << "Turn left 90 degrees\n"; for (int direction = 0; direction < 3; direction++) { if (!found && !DeadEnd()) // skip search if false found = TraverseMaze(); cout << "Turn right 90 degrees\n"; } // end for each direction cout << "Step forward, then turn 180 degrees\n"; } // end recursive step Monday, September 6 CS 215 Fundamentals of Programming II - Lecture 6 14

  15. Implementation  Finally, return the result return found;  A question:  Suppose the maze contains 10 rows and 20 columns. What is the maximum number of recursive calls that can be generated if you start at the entrance of the maze and call TraverseMaze? Monday, September 6 CS 215 Fundamentals of Programming II - Lecture 6 15

  16. Exhaustive Search  TraverseMaze is an example of the recursive pattern: exhaustive search with backtracking  Good for search problems where space is characterized by "points" with "connections" between them, and actions may be applied in any "direction". Monday, September 6 CS 215 Fundamentals of Programming II - Lecture 6 16

  17. Exhaustive Search  General structure of this pattern  Mark the current point in some way. This is to prevent returning to a point that has been visited before and going around in circles.  Check whether the current point satisfies the goal of the search. If so, return an indicator of success.  Otherwise, examine the other points connected to the current point. For each point,  Check if it is marked and ignore it if it is.  Otherwise, make a recursive call to continue the search from the connected point. If it succeeds, ignore the rest of the points and return an indicator of success. If it fails, continue with the next point. If the search of all connected points fail, return an indicator of failure. Monday, September 6 CS 215 Fundamentals of Programming II - Lecture 6 17

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend