Homework 9
22C:21 Computer Science II: Data Structures
Due Friday, April 9, 2008
1. (3 points) Exercise 9.1, page 373.
2. (3 points) Exercise 9.7, part a, page 374.
3. (4 points) Exercise 9.15, page 373.
4. (25 points)
Implement a program to find solution paths in mazes.
For this problem, mazes will be restricted to rectangular grids
of cells, like those discussed in Section 8.7 (Skim this section
of the book. You do not need to understand nor implement
the maze creation method, just the maze representation.)
There are three differences between the mazes described Section 8.7 and those that you must be able to handle:
- the starting and ending points will not always be upper left and bottom right.
Starting and ending cells will be provided as program arguments.
- walls between cells are made of one of three materials: carbon-fiber nanotubes, bricks, and adobe.
The carbon fiber walls are inpenetrable.
- there might be more than one path between any cells. The method of 8.7 ensures
that there is at most one wall-free path between any pair of cells. This corresponds
to a graph with no cycles. You must be able to handle more the more general situation, where
more than one path between cells might exist (this corresponds to having graphs that can
have cycles)
Your program must take five arguments
- mazeFileName: name of the file describing the maze to solve.
I will provide such a program that you can use to generate test mazes for your maze solving program.
- brickCost: the cost of breaking down a brick wall between two cells. This base unit of
cost is the energy used to travel between two adjacent cells that have no wall between them. Thus,
if brickCost is 5, it the cost of breaking down a brickWall and travelling to the adjacent
cell would be 6.
- adobeCost: the cost of breaking down an adobe wall between two cells.
- startCell: the index of the cell where you must begin.
- endCell: the index of the goal cell.
Your program should output the lowest cost path from the startCell to the endCell.
You should represent the maze using a weighted undirected graph, as follows:
- Each maze cell corresponds to a graph vertex. Cells are numbered as in Section 8.7;
the upper left corner being cell/vertex 0, the cell to the right of cell 0 is cell 1, etc.
- If there is no wall between two adjancent cells, there should be an edge of weight 1
between the corresponding graph vertices.
- If there is a brick or adobe wall between cells, there should be an appropriately
weighted edge between the corresponding vertices.
The input file format for the maze is as follows:
- the first line of the file contains two numbers, the number of rows and the number of columns in the maze
- each subsequent line is of the form, cell1 cell2 wallType, where
cell1 and cell2 are cell numbers and wallType is one of "none", "brick", or "adobe".
Here are a few sample maze files: smallmaze3.text, maze7x11.text,
maze1.text, maze2.text.
Note: Code to generate maze files and also to graphically display and interact with mazes: mazeio.jar, TestMazeIO.java. To use these files in Eclipse, do:
- create a project containing the TestMazeIO.java code (later, you can add your solving code to this project, but
test this project by itself first)
- Select "Project", then "Properties", then "Java Build Path", then click on the "Libraries" tab, and then on
the "Add External JARs..." button.
Navigate until you find the place where you put the "mazeio.jar" file, click on it and then click "Open" and, finally, the "OK" button.
- If you now run the "TestMazeIO" project a window with small maze should appear. If it does, you are all set - next you should read the comments
in MazeIO.java to see how to run various examples.
Submit your program (with sample output and, of course, the required README file)
via ICON.