Originally posted 6 October 2011, released 10 October 2011, due at 11:59 p.m. EDT on Monday 17 October 2011, by placing .java files and .class files in your cs187 directory on your edlab account. Please place files for this assignment in a subdirectory of cs187 called "project4".
As we get questions on this assignment we will put answers on the Q&A page.
Clarification in brown made 12 October 2011.
There is a sample driver for this project here , along with the expected output from this driver here. (Your code should not throw any exceptions when the driver is run.)
Passing these tests is a necessary, but not sufficient, condition for being correct. Your code will be tested with another driver as well, and we will downgrade you if you use algorithms that are vastly slower than necessary, even if it has the correct input/output behavior.
Goals of this project:
In this project we will rewrite the Maze
class from
Project #2
so that it has the same functionality (in particular the methods isPath
and path
but uses a different algorithm. You may use
either your own code for Project 2, a modified version of that code, or the
solutions that we posted. (The latter would provide a very useful exercise in
reading code in order to understand it well enough to modify.)
We will define a new class QCell
extending SCell
,
so that a QCell
object has two more fields, an int
called distance
and a QCell
called parent
. We will give you the
code for this class by placing it in the cs187 directory.
The breadth-first search algorithm, as described in Tuesday's lecture, is as follows. We begin by placing the source QCell on the queue, with distance 0 and parent null. As before, whenever we put a cell into the queue we mark it as seen. We then remove QCells from the queue until we either find the destination there or empty the queue. When we remove a QCell from the queue, we use the moves method to find its open unseen neighbors, if any, and place these on the queue. Each of those gets its parent set to the current node and its distance set to one more than the current node's distance.
Clearly if we empty the queue, there is no path to the destination. If we find the destination, there is a path, but unlike in Project #2 (when it was on the stack) we do not have it stored. However we can get it fairly easily to put on the array. We have assigned a distance and parent to the destination, which tells us how many cells are in the path, what the last cell in the path is, and how to find the preceding cell. That cell has a parent, which is the cell before it, and so forth until we can put the source in the array and output the array.
Remember that when you are done, you need to clean up the new
QCell
fields, setting parent
to
null
and distance
to 0, as you set all the
cells to be unseen.
You should include the classes Maze
, Cell
,
SCell
, and QCell
. You should import
java.util.*
so that you have access to the standard Java interface
Queue
and one of its implementations. (The most natural ones to
use are probably ArrayDeque
or LinkedList
,
but your code should work if we
changed it to use any class that implements Queue
.)
(Also remember that the names of the standard methods in
Queue
are not the same as the names in L&C's
QueueADT
. You may want to take advantage of the
offer
and poll
methods in
Queue
, which don't throw exceptions when the regular
methods would but instead return special values.)
Please be sure that your code compiles -- we can give partial credit for compilable code with limited functionality.
You may borrow code from any of L&C's classes, or from our solutions, with specific attribution in a comment.
Last modified 12 October 2011