Unformatted text preview:

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 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 2OutlineRecursive patternsSearching a mazeExhaustive search with backtrackingMonday, September 6 CS 215 Fundamentals of Programming II - Lecture 6 3In-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 4A 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 5Divide 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 6Searching 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 7Problem 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 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 10Design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 11DesignRecursive 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 12Implementation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 13ImplementationTraverseMaze 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 14ImplementationBase 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

UE CS 215 - Lecture 6

Documents in this Course
Lecture 4

Lecture 4

14 pages

Lecture 5

Lecture 5

18 pages

Lecture 7

Lecture 7

28 pages

Lecture 1

Lecture 1

16 pages

Lecture 5

Lecture 5

15 pages

Lecture 7

Lecture 7

28 pages

Load more
Download Lecture 6
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 6 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 6 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?