当使用更大的数组维度时,我已经将程序的终止隔离为始终在2D顶点顶点数据的广度搜索期间:
我的Vertex类被声明为
class Vertex{
public:
int i, j;
std::set<Vertex*> adj; //references to adjacent vertices (max of 4)
//used in solving maze
bool visited;
std::list<Vertex> path; //stores path from start vertex to this vertex
Vertex();
~Vertex();
//end constructors
void setPos(int row, int col);
/** for iterators */
typedef std::set<Vertex*>::iterator iterator;
iterator begin();
iterator end();
};//END class Vertex
在这个函数中,我有这个小节来执行BFS:
void solveMaze(string folder, Vertex arr[][MAZE_WIDTH]){
//<randomly generate begin and end positions in maze>
//<ensure all vertices are marked unvisited>
/* BFS */
list< Vertex > shortestPath; //shortest path from start to end
queue<Vertex*> q; //store visited vertices with one or more unvisited adjacent vertices while searching graph
arr[ begini ][ beginj ].visited = true; //mark start vertex as visited, as the search will begin here
arr[ begini ][ beginj ].path.push_back( arr[ begini ][ beginj ] ); //all paths begin at start vertex
q.push( &arr[begini][beginj] );
//explore all vertices in maze
while( !q.empty() ){
Vertex* cur = q.front(); //next vertex to backtrack
q.pop();
//for all vertices adjacent of cur
Vertex::iterator it; //thanks to typedef
for(it = (cur->begin()); it != (cur->end()); it++){
//if adjacent vertex has not been visited
if( !(*it)->visited ){
(*it)->visited = true; //mark vertex visited
(*it)->path = cur->path; //save directory from start vertex..
cout << "1\n";
((*it)->path).push_back( **it ); //..to this vertex i think this line is main problem
cout << "2\n";
q.push( &arr[ ((*it)->i) ][ ((*it)->j) ]); //current path continues, so store vertex to backtrack later
}
}
//path to end found, so store it if it's shortest found
if( (cur->i) == endi && (cur->j) == endj ){
if( shortestPath.empty() )
shortestPath = ( cur->path );
else if( (cur->path).size() < shortestPath.size() )
shortestPath = cur->path;
}
}//end while( !q.empty() )
cout << " -finished breadth first searching\n";
...
所以在BFS期间,1 2 1 2 ...重复打印,但是在最后1次打印之前越来越慢,然后它会终止并出现如下错误:
...
1
2
1
2
1
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
make: *** [makeExec] Error 3
所以我怀疑这是我用指针/迭代器使用等做错的事情,比如我在上面的while循环中的行“((* it) - &gt; path).push_back(** it);”因为1最后打印然后它终止,但它只在使用更大的数组维度时发生。
有人可以帮我弄清楚发生了什么吗?
答案 0 :(得分:0)
更改
std::list<Vertex> path; //stores path from start vertex to this vertex
到
std::list<Vertex*> path; //stores path from start vertex to this vertex
工作得很漂亮。应该知道我浪费的资源,谢谢@uesp!