Monday, September 6 CS 215 Fundamentals of Programming II - Lecture 6 1
Lecture 6
Log into Linux Does everyone have the in-class exercise from
last class (factal.cpp)? Questions?
Questions about Homework 3?
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
Monday, September 6 CS 215 Fundamentals of Programming II - Lecture 6 1
Log into Linux Does everyone have the in-class exercise from
Questions about Homework 3?
Monday, September 6 CS 215 Fundamentals of Programming II - Lecture 6 2
Recursive patterns Searching a maze Exhaustive search with backtracking
Monday, September 6 CS 215 Fundamentals of Programming II - Lecture 6 3
Modify RandomFractal so that the movements
Compile, run, and test your program. When you are done, print out your program and
Monday, September 6 CS 215 Fundamentals of Programming II - Lecture 6 4
Suppose a program has the following call:
This original call will generate two recursive
Monday, September 6 CS 215 Fundamentals of Programming II - Lecture 6 5
RandomFractal is an example of the recursive
General structure of this pattern
Check for the base case Divide the problem into two equal-sized problems
Combine the two results into the result for the
Monday, September 6 CS 215 Fundamentals of Programming II - Lecture 6 6
A maze is a puzzle in the form of a complex
We will assume that our maze is built on a
Monday, September 6 CS 215 Fundamentals of Programming II - Lecture 6 7
Write a function TraverseMaze that can be
The function assumes that the user is facing an
The function returns true if the user has found
Monday, September 6 CS 215 Fundamentals of Programming II - Lecture 6 8
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? No Is the ground ahead marked? No X
Monday, September 6 CS 215 Fundamentals of Programming II - Lecture 6 9
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 You found it! X
Monday, September 6 CS 215 Fundamentals of Programming II - Lecture 6 10
Goal: explore maze in this direction; at end of
Always have the user step forward into the new
Base case:
Condition: the user has found the treasure Action: pick up treasure and step backwards (the
Monday, September 6 CS 215 Fundamentals of Programming II - Lecture 6 11
Recursive step
Monday, September 6 CS 215 Fundamentals of Programming II - Lecture 6 12
Copy files /home/hwang/cs215/lecture06/*.*
Helper function:
DeadEnd ( ) - asks user if the way to the next spot
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 13
TraverseMaze neither receives nor passes
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 14
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 15
Finally, return the result
return found;
A question:
Suppose the maze contains 10 rows and 20
Monday, September 6 CS 215 Fundamentals of Programming II - Lecture 6 16
TraverseMaze is an example of the recursive
Good for search problems where space is
Monday, September 6 CS 215 Fundamentals of Programming II - Lecture 6 17
General structure of this pattern
Mark the current point in some way. This is to prevent
Check whether the current point satisfies the goal of the
Otherwise, examine the other points connected to the
Check if it is marked and ignore it if it is. Otherwise, make a recursive call to continue the search