Lecture 6OutlineIn-class Exercise, Last ClassA QuestionDivide and ConquerSearching a MazeProblem SpecificationExample Run 1Example Run 2Design 1Design 2Implementation 1Implementation 2Implementation 3Implementation 4Exhaustive Search 1Exhaustive Search 2Monday, September 6 CS 215 Fundamentals of Programming II - Lecture 6 1Lecture 6Log into LinuxDoes 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 2OutlineRecursive patternsSearching a mazeExhaustive search with backtrackingMonday, September 6 CS 215 Fundamentals of Programming II - Lecture 6 3In-class Exercise, Last ClassModify 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 4A QuestionSuppose 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 5Divide and ConquerRandomFractal is an example of the recursive pattern: divide and conquerGeneral structure of this patternCheck for the base caseDivide 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 6Searching a MazeA 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 7Problem SpecificationWrite 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 8Example RunStep forward & mark the groundHave you found the treasure?NoTurn left 90 degreesAre you facing a wall?NoIs the ground ahead marked?NoStep forward & mark the groundHave you found the treasure?NoTurn left 90 degreesAre you facing a wall?YesTurn right 90 degreesAre you facing a wall?NoIs the ground ahead marked?NoXMonday, September 6 CS 215 Fundamentals of Programming II - Lecture 6 9Example RunStep forward & mark the groundHave you found the treasure?YesPick up the treasure and step backwardTurn right 90 degreesTurn right 90 degreesStep forward, then turn 180 degreesTurn right 90 degreesTurn right 90 degreesTurn right 90 degreesStep forward, then turn 180 degreesYou found it!XMonday, September 6 CS 215 Fundamentals of Programming II - Lecture 6 10DesignGoal: explore maze in this direction; at end of function, user is standing exactly like they were at the beginning with indicator of successAlways have the user step forward into the new spot and mark the ground at the start and return the result of search at the endBase case: Condition: the user has found the treasureAction: 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 11DesignRecursive step1. Have the user turn left2. 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 12ImplementationCopy files /home/hwang/cs215/lecture06/*.* from csserver. Examine maze.cppHelper 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 13ImplementationTraverseMaze neither receives nor passes back any data, so it has no parameters; it returns a boolbool TraverseMaze ();Local variables: found – set to true when treasure has been found;direction – integer to count the directions.Step forward and mark the groundcout << "Step forward & marked the ground\n";Monday, September 6 CS 215 Fundamentals of Programming II - Lecture 6 14ImplementationBase case:bool found = inquire ("Have you found the treasure?");if (found) cout << "Pick up the treasure and step backward\n";Recursive stepelse { 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 stepMonday, September 6 CS 215
View Full Document