C++ Maze Solving algorithm segmentation fault

时间:2015-10-30 22:00:46

标签: c++

The task is to create a randomly generated maze and then solve it, the issue is that whenever I recursively search the maze for the exit, i get a segmentation fault at runtime. Most of the cout is for debugging.

The Main File (where error persists)

#include <string>
#include <cmath>
#include <cstdlib>
#include "Main.h"
#include "Maze.h"

int main(int argc, char *argv[])
{
      // Process the command line arguments and seed the random number
      // generator, if necessary
    unsigned int seed;
    if(argc == 4)
    {
        seed = static_cast<unsigned int>(atoi(argv[1]));
        cout << "Initializing pseudorandom number generator with seed "
             << seed << endl;
        srand(seed);
    }
    else if(argc > 4)
    {
        cout << "Improper usage of Main\n";
        exit(0);
    }

    cout << "Beginning execution\n";

        // The first couple of numbers don't seem quite random,
        // so "prime the pump" by calling rand() twice
    (void) rand(); (void) rand();

    mazeTest();

    cout << "Finishing execution\n";

    return 0;
}

void mazeTest()
{
    int height = 0;
    int width = 0;

    cout << "Enter height (positive integer): ";
    cin >> height;
    cout << height << "\n";
    cout << "Enter width (positive integer): ";
    cin >> width;
    cout << width << "\n";

    MazeBuilder theMazeBuilder(height, width);
    cout << "CREATED MAZE BUILDER\n";
    Maze theMaze(theMazeBuilder);
    cout << "CREATED THE MAZE\n";

    cout << "The Maze:\n" << theMaze.toString();
    solveMaze(theMaze);
}

void solveMaze(const Maze& theMaze)
{
    cout << "ENTERED solveMaze()\n";
    thePath = new int[theMaze.getHeight()*theMaze.getWidth()];

    bool **wasHere;

    for(int i = 0; i < theMaze.getHeight(); i++)
    {
        for(int j = 0; i < theMaze.getWidth(); j++)
        {
            wasHere[i][j] = false;
        }
    }

    cout << "PATH INITIALIZED\n";
    if(search(theMaze, theMaze.getEntranceRow(), 0, wasHere, thePath, thePathLength))
    {
        theMaze.toString(thePath, thePathLength);
    }
}

bool search(const Maze& theMaze, int row, int col, 
                bool**& wasHere, int *aPath, int currentPathLength)
{
    if(col == theMaze.getWidth()-1 && row == theMaze.getExitRow())
    {
        cout << "FOUND EXIT\n";
        thePathLength = currentPathLength;
        thePath = aPath;
        return true;
    }

    if(wasHere[row][col] == true)
        return false;
    wasHere[row][col] = true;

    if(row != 0 && theMaze.at(row,col).isWall(UP) == false)
    {
        if(search(theMaze, row-1, col, wasHere, aPath, currentPathLength))
        {
            aPath[currentPathLength] = UP;
            currentPathLength++;
            cout << "UP\n";
            return true;
        }
    }

    if(col != theMaze.getWidth()-1 && theMaze.at(row,col).isWall(RIGHT) == false)
    {
        if(search(theMaze, row, col+1, wasHere, aPath, currentPathLength))
        {
            aPath[currentPathLength] = RIGHT;
            currentPathLength++; 
            cout << "RIGHT\n";
            return true;
        } 
    }

    if(row != theMaze.getHeight()-1 && theMaze.at(row,col).isWall(DOWN) == false)
    {
        if(search(theMaze, row+1,col, wasHere, aPath, currentPathLength))
        {
            aPath[currentPathLength] = DOWN;
            currentPathLength++;
            cout << "DOWN\n";
            return true;
        }
    }

    if(col != 0 && theMaze.at(row,col).isWall(LEFT) == false)
    {     
        if(search(theMaze, row, col-1, wasHere, aPath, currentPathLength))
        {
            aPath[currentPathLength] = LEFT;
            currentPathLength++;
            cout << "LEFT\n";
            return true;
        }            
    }

    cout << "DEAD END\n----------------------------\n";
    return false;
}

Important methods used...

Maze::at(int row, int col) returns the cell at given row and column

Maze::toString() or toString(int* thePath, int thePathLength) default: prints out just the maze using ASCII characters to cmd with parameters: Prints out maze with solution using ASCII characters to cmd

Cell::isWall(direction) returns whether or not there is a wall in that direction (directions are constants declared and handled in Cell)

Other Info: Maze is a dynamic 2d array of Cells The maze is constructed properly (can output an unsolved maze properly)

1 个答案:

答案 0 :(得分:4)

The problem:

bool **wasHere;

for(int i = 0; i < theMaze.getHeight(); i++)
{
    for(int j = 0; i < theMaze.getWidth(); j++)
    {
        wasHere[i][j] = false;

The reason: wasHere is an uninitialized pointer to a pointer to a bool. You then dereference the first pointer causing undefined behavior (such as perhaps a crash...).

相关问题