CSC 161 Schedule Readings Labs & Projects Homework Deadlines Resources

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

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:

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

Here are two small examples:
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

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.

A five point penalty will apply to any submission that includes personally identifying information anywhere other than the references/honesty file.