所以这里有另一个学校问题。这是我的迷宫解决代码,它相当标准,基于深度搜索。该代码似乎运行良好,但是当我到达迷宫的尽头时,我遇到了分段错误。我真的很难知道这是从哪里来的。
所以这是解决 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 输出,下一个函数是实际调用上述函数的函数,如果成功创建一个具有解决方案坐标的数组
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 << "starting to convert to string vector" << endl;
所以我很不知道这个分段错误来自哪里。任何输入或想法都会很棒!
谢谢!
控制台输出在最后(我省了你整个事情,但只是为了表明它一直工作到最后,它达到了基本情况并出现了段错误,为什么!!!?
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 < 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;
}
如您所见,findPath 是从 if 语句中的 mazeSolCoordinates 调用的。