我正在做一个家庭作业,为滑动拼图实现A * . 我之前已经为这个谜题实施了BFS和DFS,但现在遇到了问题 . 我试图通过调整我已使用优先级队列和曼哈顿距离作为启发式工作的BFS代码来编写我的A *代码 . 为了让程序打开具有最低f值(g h)的节点,我必须覆盖优先级队列的运算符,因为它包含定义的结构 . 当我编译代码时,不会出现错误,但是当我运行它时程序就崩溃了 . 我的猜测是它在某个地方陷入了困境 . 也许我错误地实现了操作符覆盖 . 谁能帮我弄清楚出了什么问题?我在下面粘贴了我的代码 .

#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <list>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <time.h>

using namespace std;

struct move //creates structure for a move containing a pice number and direction to move
{
    int piece;
    char direction;
};

struct parentmove
{
    vector< vector<int> > par;
    move mo;
};

struct position
{
    int row;
    int column;
};

struct node
{
    vector< vector<int> > par;
    vector< vector<int> > state;
    position pos;
    int g;
    int h;
    int f;
};

bool operator<(const node& a, const node& b)
{
    return a.f > b.f;
}

void SBP::Astar(vector <vector<int> > st)
{
    position goalpos = getposition(st, -1);
    node stnode;
    stnode.state = st;
    stnode.g = 0;
    position stpos = getposition(st, 2);
    stnode.h = abs(goalpos.row - stpos.row) + abs(goalpos.column - stpos.column);
    stnode.f = stnode.g + stnode.h;
    stnode.pos = stpos;
    list<move> allm;
    priority_queue< node > unexplored, explored;
    set< vector <vector<int> > > closedset; //contains explored states in a set, 
    map < vector <vector<int> >, parentmove > pathdict;
    allm = (*this).allmoves(st);
    list<move>::iterator it = allm.begin();
    for (int i = 0; i < allm.size(); i++)
    {
        move m;
        m.piece = (*it).piece;
        m.direction = (*it).direction;
        vector< vector<int> > newst = (*this).applyMove(st, m);
        newst = (*this).normalization(newst);
        node newnode;
        newnode.par = st;
        newnode.state = newst;
        newnode.g = 1;
        position newpos = getposition(newst, 2);
        newnode.h = abs(goalpos.row - newpos.row) + abs(goalpos.column - newpos.column);
        newnode.f = newnode.h + newnode.g;
        newnode.pos = newpos;
        unexplored.push(newnode);

        parentmove pm = { st,m };
        pathdict[newst] = pm;
        ++it;
    }
    explored.push(stnode);
    closedset.insert(st);
    while (!unexplored.empty())
    {
        node currentnode = unexplored.top();
        vector< vector<int> > currentst = currentnode.state;
        unexplored.pop();
        bool iscomplete = (*this).complete(currentst);
        if (iscomplete)
        {
            explored.push(currentnode);
            closedset.insert(currentst);
            list<move> path = (*this).constructpath(currentst, pathdict, st);
            (*this).display(currentst);
            printf("A* explored %i nodes\n", explored.size());
            printf("The length of the path solution is %i moves\n", path.size());
            return;
        }
        else
        {
            set< vector< vector<int> > >::iterator ite = closedset.find(currentst);
            if (ite == closedset.end()) //does not already exist in queue
            {
                list<move> possm;
                possm = (*this).allmoves(currentst);
                list<move>::iterator iter = possm.begin();
                for (int j = 0; j < possm.size(); j++)
                {
                    move m;
                    m.piece = (*iter).piece;
                    m.direction = (*iter).direction;
                    vector< vector<int> > newst = (*this).applyMove(currentst, m);
                    vector< vector<int> > normst = (*this).normalization(newst);;
                    node normnode;
                    normnode.state = normst;
                    normnode.par = currentst;
                    normnode.g = currentnode.g + 1;
                    position p = getposition(normst, 2);
                    normnode.h = abs(goalpos.row - p.row) + abs(goalpos.column - p.column);
                    normnode.f = normnode.g + normnode.h;
                    normnode.pos = p;
                    unexplored.push(normnode);
                    if (pathdict.find(normst) == pathdict.end())
                    {
                        parentmove pm = { currentst,m };
                        pathdict[normst] = pm;
                    }
                    ++iter;
                }
                explored.push(currentnode);
                closedset.insert(currentst);
            }
        }
    }

    return;
}