我正在使用优先级队列编写统一成本搜索的代码。我遇到了一个错误:使用推送功能后无效的堆。我也尝试调试代码,但我没有找到此错误的来源。这是我的代码:
class Manager
{
private:
Parser *p;
Board* myBoard;
string algorithm;
string text;
unsigned int size;
stack<Brick> *myStack;
stack<Brick> *solution;
stack<Brick> *temp;
unsigned int trackCost;
set<pair<int,int>> mySet;
struct ComparatorGreaterThan {
bool operator() (const Brick lhs, const Brick rhs) {
return (lhs.getTrackCostUpToMe() > rhs.getTrackCostUpToMe());
}
};
public:
Manager();
~Manager();
void Start();
void parseText();
void presentBoard() const;
const void checkNeighbours( int i, int j,const int x,const int y,stack<Brick> *tempStack, const Brick father)const;
void returnNeighbours(const Brick node, stack<Brick>* tempStack,const Brick father);
bool DFS(const Brick node,const int depth,const Brick father)const;
void parseSolution();
void IDDFS();
void printSolution()const;
bool UCS();
};
这是UCS功能:
bool Manager::UCS()
{
priority_queue<Brick,vector<Brick>,ComparatorGreaterThan> myQueue;
myQueue.push(myBoard->getRoot());
mySet.insert(myBoard->getRoot().getCoorddinates());//Set is used to know which elements,using the coordiantes, are in the PQ
while (!myQueue.empty())
{
Brick parent = myQueue.top();
myQueue.pop();
mySet.erase(parent.getCoorddinates());
if (parent.getSym() == 'G')//If goal is found
{
cout << parent.getTrackCostUpToMe();
return true;
}
stack<Brick> tempStack;//Keeps all the neighbours of current node.
returnNeighbours(parent, &tempStack,myBoard->getNode(parent.getFatherX(),parent.getFatherY()));//Finds all the neighbours of current node.
while (!tempStack.empty())
{
tempStack.top().setTrackCostUpToMe(parent.getTrackCostUpToMe());//Set the track cost of neighbour.
if (mySet.find(tempStack.top().getCoorddinates()) == mySet.end())//If we don't have neighrbour in the PQ
{
mySet.insert(tempStack.top().getCoorddinates());
myQueue.push(tempStack.top());
tempStack.pop();
}
else //We have the neighbour in the PQ
tempStack.pop();
}
}
return false;
}