当前位置: 首页>>代码示例>>C++>>正文


C++ PathVector::insert方法代码示例

本文整理汇总了C++中PathVector::insert方法的典型用法代码示例。如果您正苦于以下问题:C++ PathVector::insert方法的具体用法?C++ PathVector::insert怎么用?C++ PathVector::insert使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在PathVector的用法示例。


在下文中一共展示了PathVector::insert方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: findPath

void Algorithm::findPath(const Location& start, const Location& end,
                         PathVector& output) {
    NodeMap l_open;
    NodeMap l_closed;
    std::priority_queue<Node> l_priority;

    // Create Start Node
    Location origin;
    Node startNode(start, 0, 0, origin);

    // Add start to priority queue and open set
    l_priority.push(startNode);
    l_open[start] = startNode;

    while (!l_priority.empty()) {
        Node l_current = l_priority.top();
        l_priority.pop();

        l_open.erase(l_current.location);
        l_closed[l_current.location] = l_current;

        // Check whether this is the target node
        if (l_current.location == end) {
            Location current = l_current.location;
            while (current != start) {
                // output.push_back (index);
                output.insert(output.begin(), current);
                NodeMapIter open_iter = l_closed.find(current);
                current = open_iter->second.origin;
                if (current.x == UINT_MAX)
                    break;
            }
            return;
        }

        // Process neighbours
        Location* neighbours = new Location[m_numNeighbours];
        unsigned int validNeighbours =
            m_neighbourFunction(l_current.location, neighbours, m_customData);
        for (size_t ii = 0; ii < validNeighbours; ii++) {
            // TODO: not needed? if (neighbours[ii] < 0) continue; // Not a
            // valid neighbour
            int cost = m_costFunction(neighbours[ii], m_customData);
            // If cost is -ve, we will close the node

            unsigned int path = l_current.distance + cost;
            unsigned int priority =
                path + m_distanceFunction(neighbours[ii], end, m_customData) +
                1;
            Node neighbour(neighbours[ii], path, priority, l_current.location);

            // Is it closed?
            NodeMapIter closed_iter = l_closed.find(neighbours[ii]);
            if (closed_iter != l_closed.end()) {
                // But dow we now have a better path?
                if (cost > 0 && path < closed_iter->second.distance) {
                    l_closed.erase(closed_iter);
                } else {
                    continue; // It's closed and there's no better path
                }
            }

            NodeMapIter open_iter = l_open.find(neighbours[ii]);
            if (open_iter != l_open.end()) {
                // Remove it from open if there's a better path now
                l_open.erase(open_iter);
            } else if (cost >= 0) {
                // Neighbour not in open
                l_open[neighbours[ii]] = neighbour;
                l_priority.push(neighbour);
            }
        }
        delete[] neighbours;
    }
}
开发者ID:pkuehne,项目名称:fortress,代码行数:75,代码来源:algorithm.cpp


注:本文中的PathVector::insert方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。