Maze Solving
- Summary
- You search a maze for a path to the goal.
- Objectives
-
-
Practice using
- structures,
- dynamic memory allocation,
- linked lists, and
- stacks
- Reinforce knowledge of arrays, pointers
- Improve code-reading skills
-
Practice using
Introduction
Both dynamic programming (which you'll study in CSC 301) and divide-and-conquer, (which you saw in CSC 151, with merge sort and binary search) are examples of general families of algorithmic approaches. Another family of algorithms involves backtracking, in which a set of partial solutions are built-up and eventually abandoned if they fail to reach the goal. In this assignment, you will use a highly simplified version of backtracking to explore a maze.
The simplified algorithm for maze-solving involves using a stack to keep track of maze locations that have not yet been visited, but which are adjacent to those that have been. (A complete solution would also store the complete list of steps required to reach that cell.)
The basic strategy proceeds as follows. Begin by pushing the start location onto a stack. Then, as long as the stack is not empty, you pop the top and see if it's the end/goal state. If it is, you're done. If not, you mark the state as visited and push the popped location's unvisited neighbors onto the stack and repeat.
In a grid, the neighbors will be to the north, south, east, and west. However, some of those locations may be blocked by a wall, so they should only be pushed for later consideration if they are in fact open.
Details
Your program will accept a maze in the format described below either via standard input or a file name given on the command line.
The task of reading input can be somewhat tedious, so these capabilities are provided for you. (Plus, it is essential practice in learning to read documentation to use others' code.)
Implementing the path-finding operation will require a library to
maintain a stack of positions. You must develop a list-based stack
with the operations necessary for this problem using a separate
header and program file, using a Makefile to link the
various objects.
Your driver program solve.c should therefore:
- Read a maze
- Traverse the maze using the stack-supported algorithm sketched in the introduction
- Mark the cells as visited during the traversal
- Print the resulting maze once the options are exhausted or the goal is found
Maze Format
The first line of a maze file should be the dimensions of the grid—two positive integers, representing the height and width, respectively.
The maze grid is given next. It will consist of an H×W matrix of characters, where
- a space represents an open cell,
Srepresents the start cell,Erepresents the end cell, and- any other character represents a blocked cell.
13 41 *********************S******************* * * * * * *** * *** *** ******* * ******* * ***** * * * * * * * * * * * * * * * *** ******* * * ***** *** * * * * *** * * * * * * * * * * * * * * * *** * *********** * *** * * * * *** * * * * * * * * * * * * * * * * * ******* * *** * * ***** * * *** *** * * * * * * * * * * * * * * * * *** *** * * * * ******* *** * *** *** * * * * * * * *********************E*******************
10 20 #################### #S# #E # # # ####### #### # # # # # # ### # ##### ### # # # # # ####### ## # # # ### # # # # # # # # # ## # # # # # # ####################Maze from Figure 9.8 (p. 232) in Java Structures (McGraw-Hill) by Duane A. Bailey.
Links to the code below give you a program that will automatically generate random mazes, with a little manual annotation.
Code
-
position.hhas a struct encoding row, column pairs (to be used in your stack implementation) -
maze.h/maze.creads and prints mazes. (You need not readmaze.cclosely.) -
(Optional)
amaze.cgenerates mazes, but you must add the dimensions to the result (use 2*N+1 in your file, because the generator counts differently) and mark the start and end states.
Example
An example run using the second maze above might print the following output. (Your results will necessarily depend on the order in which cells are explored.)
XXXXXXXXXXXXXXXXXXXX X@X X$...X...X X.XXXXXXX XXXX.X.X.X X.........X..X.XXX.X XXXXX XXX.X........X X X X...XXXXXXX.XX X X X.XXX X...X..X X X X.X...X.X.XX.X X X...X...X....X XXXXXXXXXXXXXXXXXXXX
Grading
In addition to the general grading guidelines for evaluation, the assignment is worth 25 points.
- [2 points] Functions give clear, complete pre- and post-conditions
- [4 points] List-based stack library provides empty, top, push, and pop operations
- [1 points] Assertions verify pre-conditions
- [2 points] Program frees all allocated memory
- [6 points] Conducts a stack-based search
- [1 point] Search visit start cell first
- [1 point] Search stops at end cell
- [1 point] Search expands each position at most once
- [3 points] Search explores all valid neighbors
- [4 points] Program library and compilation managed separately
- [2 points] Stack library separated from main program
- [2 points]
Makefilecompiles and links objects separately - [6 points] Tests demonstrate correctness
- [2 points] Test plan enumerates the range of problem circumstances
- [2 points] Test cases include specific inputs and expected outcome(s)
- [1 point] Transcripts demonstrate functionality with test runs
- [1 point] Statement argues why the program is correct
A five point penalty will apply to any submission that includes personally identifying information anywhere other than the references/honesty file.
