UNIX中的分段错误,但不是Windows

时间:2018-03-09 10:10:04

标签: c++ unix segmentation-fault

我正在尝试让我的DFS代码在UNIX中运行,但每当我运行它时,它会给我一个分段错误,但它在Windows中运行良好。该代码应该接受一个字符串,并通过一次移动一个字母来对其进行排序以达到某个预定义的目标状态。我在UNIX方面没有太多经验,所以我非常感谢对此问题的一些见解。谢谢!

struct node
{
    node* parent;
    string key;
    int cost;
    int heuristicCost;

    node()
    {
        parent = nullptr;
        key = "key";
        cost = 0;
        heuristicCost = 0;
    }
};

void BFS(string sortString)
{
    // Create Queue
    list<node*> queue;
    list<string> explored;
    // Add the initial case to the Queue
    node *n = new node;
    n->key = sortString;
    queue.push_back(n);
    int steps = 1;
    while (!queue.empty())
    {
        // Print out the current node to be checked and the operation that was performed on it
        for (int i = 0; i < (int)queue.front()->key.length(); i++)
        {
            if (queue.front()->key[i] == 'X')
            {
                if (steps == 1)
                {
                    cout << queue.front()->key << endl;
                    steps++;
                }
                else
                {
                    cout <<  "move " << i << " " << queue.front()->key << endl;
                    steps++;
                }
            }
        }
        // Check if the goal state is reached
        if (GoalTest(queue.front()->key))
        {
            cout << "Final Result for BFS:" << endl;
            node* currentNode = queue.front();
            list<node*> path;
            while (currentNode->parent != nullptr) {
                path.push_front(currentNode);
                currentNode = currentNode->parent;
            }
            int i = 1;
            while((int)path.size() >= 1)
            {
                cout << "Step " << i << ": " << path.front()->key << endl;
                i++;
                path.pop_front();
            }
            return;
        }
        else
        {
            // If goal state is not reached, add new nodes to the frontier
            explored.push_back(queue.front()->key);
            for (int i = 0; i < (int)queue.front()->key.length(); i++)
            {
                // Do a move action on the next node to be enqueued, if node is already in explored list, skip it
                string key = Move(i, queue.front()->key);
                bool found = (find(explored.begin(), explored.end(), key) != explored.end());
                if (found)
                {
                    continue;
                }
                else
                {
                    node *newNode = new node;
                    newNode->key = key;
                    newNode->parent = queue.front();
                    queue.push_back(newNode);
                }
            }
            // Remove the first node of the queue
            queue.pop_front();
            bool found = (find(explored.begin(), explored.end(), queue.front()->key) != explored.end());
            while (found)
            {
                queue.pop_front();
                found = (find(explored.begin(), explored.end(), queue.front()->key) != explored.end());
            }
        }
    }
    // If loop exits normally, search failed
    cout << "Failure!" << endl;
    return;
}

0 个答案:

没有答案