使用递归的3d迷宫 - 在递归结束时的Seg错误

时间:2013-11-07 06:18:56

标签: c++ recursion 3d fault maze

所以这是另一个学校问题。这是我的迷宫解码代码,它相当标准,基于深度搜索。代码似乎运行良好,但当我到达迷宫的尽头时,我得到了分段错误。我真的很想知道它的来源。

所以这是解决5x5 3d迷宫的实际代码

bool PathFinder :: findPath(int x, int y, int z)
{
    cout << "*****findpath called"<< endl;
    find_x = x;
    find_y = y;
    find_z = z;
    string insert_x, insert_y, insert_z;
    ostringstream stream_x, stream_y , stream_z;
    stream_x << find_x;
    stream_y << find_y;
    stream_z << find_z;
    insert_x = stream_x.str();
    insert_y = stream_y.str();
    insert_z = stream_z.str();
    cout << "working on" << insert_x << " " << insert_y << " " << insert_z << endl;
    cout << "checking to see if its in bounds" << endl;

    if (find_x < 0 || find_y < 0 || find_z < 0 || find_x > 4 || find_y > 4 || find_z > 4)
    {
        // then out of bounds!
        cout << "out of bounds" << endl;
        return false;
    }else
        cout << "checking if end" << endl;

    if (find_x == 4 && find_y == 4 && find_z == 4 && (Maze[find_x][find_y][find_z].cube_value == 1))
    {
        // then i finished successfully!
        cout << "is finish" << endl;
        return true;
    }
    else
        cout << "checking for wall" << endl;

    if (Maze[find_x][find_y][find_z].cube_value != 1 && Maze[find_x][find_y][find_z].visited != true)
    {
        // then it is a wall
        cout << "is a wall" << endl;
        Maze[find_x][find_y][find_z].visited = true;
        return false;
    }else
        cout << "checking if visited" << endl;

    if (Maze[find_x][find_y][find_z].visited == true)
    {
        // then it I have been here
        cout << "I have been here" << endl;
        Maze[find_x][find_y][find_z].visited = true;
        return false;
    }
    else
    {
        cout << "operating on valid cube" << endl;

        string to_push = "(" + insert_x + ", " + insert_y + ", " + insert_z + ")";
        cout << "          pushing" << to_push << endl;
        pathstack.push(to_push);
        Maze[find_x][find_y][find_z].visited = true;

        if(findPath(find_x,find_y,find_z + 1) && Maze[find_x][find_y][find_z + 1].visited != true)
        {
            return true;
        }
        else if(findPath(find_x,find_y,find_z-1) && Maze[find_x][find_y][find_z-1].visited != true)
        {
            return true;
        }
        else if(findPath(find_x,find_y + 1,find_z) && Maze[find_x][find_y+1][find_z].visited != true)
        {
            return true;
        }
        else if(findPath(find_x,find_y-1,find_z) && Maze[find_x][find_y-1][find_z].visited != true)
        {
            return true;
        }
        else if(findPath(find_x+1,find_y,find_z) && Maze[find_x+1][find_y][find_z].visited != true)
        {
            return true;
        }
        else if(findPath(find_x-1,find_y,find_z) && Maze[find_x-1][find_y] [find_z].visited != true)
        {
            return true;
        }
        else
        {
            cout << "removing ------------->" << pathstack.top() <<  endl;
            pathstack.pop();
            return false;
        }
    }
}

我在那里有很多c out,因为我一直在尝试调试这段代码,下一个函数是实际调用上面函数的函数,如果成功创建了一个带有解决方案坐标的数组

vector<string> PathFinder :: mazeSolCoordinates()
 {
// set up local variables
cout << "LAB MAZE SOLVER" << endl;
vector<string> returnpath;
pathstack.empty();
temporary.empty();
if(findPath(0,0,0) == true)
{

cout << "starting to convert to string vector" << endl;
pathstack.push("(4, 4, 4)");
int size = pathstack.size();
for(int count = 0 ; count < size ; count++)
{
//      cout << "reversal" << endl;
//      cout << pathstack.top() << endl;
    temporary.push(pathstack.top());
    pathstack.pop();
}
for(int count = 0 ; count < size ; count++)
{
//      cout << "adding to vector" << endl;
//      cout << temporary.top() << endl;
    returnpath.resize(count+1);
    returnpath[count] = temporary.top();
//      cout << "just added to string vector" << endl;
    temporary.pop();
//      cout << "just popped this baby of the stack" << endl;
}
find_x = 0;
find_y = 0;
find_z = 0;
return returnpath;
}else
{
    cout << "no vector to return" << endl;
    return returnpath;
}
}

我很确定第二部分工作正常,我的错误发生在我到达线之前         cout&lt;&lt; “开始转换为字符串向量”&lt;&lt; ENDL;

所以我很遗憾这个分段错误的来源。任何输入或想法都会很精彩!

谢谢!

最终的控制台输出(我省去了整个事情,但只是为了表明它一直工作到最后,它到达基本情况和seg故障,为什么!!!?

          pushing(3, 4, 4)
*****findpath called
working on3 4 5
checking to see if its in bounds
out of bounds
*****findpath called
working on3 4 4
checking to see if its in bounds
checking if end
checking for wall
checking if visited
I have been here
*****findpath called
working on3 5 4
checking to see if its in bounds
out of bounds
*****findpath called
working on3 4 4
checking to see if its in bounds
checking if end
checking for wall
checking if visited
I have been here
*****findpath called
working on4 4 4
checking to see if its in bounds
checking if end
is finish
Run_Test_Driver.sh: line 14: 27431 Segmentation fault      (core dumped) ./$EXE
Press any key to exit...

更新所以我通过XCode使用断点运行它,在执行“return true”之后/之后我收到以下错误:

_LIBCPP_ASSERT(__ n&lt; size(),“vector [] index out of bounds”);

我不太喜欢使用这个工具进行调试,或者一般来说,在我的代码中,它会在这一步之后尝试读取一个向量吗?

这里更新是我的main.cpp

  #include <iostream>
#include "PathFinder.h"
using namespace std;

int main ()
{
cout << "Hello World!";
PathFinder* me;
me = new PathFinder();
me->importMaze("Solvable1.txt");
me->getMaze();
me->solveMaze();


me->createRandomMaze();
me->getMaze();
me->solveMaze();


me->createRandomMaze();
me->getMaze();
me->solveMaze();


me->createRandomMaze();
me->getMaze();
me->solveMaze();
return 0;

}

正如您所见,在if语句中从mazeSolCoordinates调用了findPath。

0 个答案:

没有答案