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


C++ PathType::push_front方法代码示例

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


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

示例1: getPath

            PathType getPath(const GraphAdapterType& graphAdapter, const NodeAdapterType& start, const std::function<bool(const NodeAdapterType&)>& endCondition) const {
                PathType resultPath;

                struct NodePositionComparator {
                    bool operator() (const NodeAdapterType& lhs, const NodeAdapterType& rhs) {
                        return lhs.position < rhs.position;
                    }
                };

                std::set<NodeAdapterType> open = { start };
                std::set<NodeAdapterType, NodePositionComparator> closed;
                std::map<NodeAdapterType, NodeAdapterType, NodePositionComparator> came_from;
                typename std::set<NodeAdapterType>::iterator current;

                while(open.empty() == false) {
                    current = open.begin();

                    if(endCondition(*current) == true) {
                        // Goal found, recreating path from 'goal' node to 'start' node
                        resultPath.push_front(current->position);
                        auto pathCurrent = came_from.find(*current);
                        if(pathCurrent != came_from.end()) {
                            while(pathCurrent->second.position != start.position) {
                                resultPath.push_front(pathCurrent->second.position);
                                pathCurrent = came_from.find(pathCurrent->second);
                            }
                        }

                        return resultPath;
                    }

                    closed.insert(*current);

                    std::vector<NodeAdapterType> neighbours = graphAdapter.getNeighboursOf(*current);
                    for(NodeAdapterType& neighbour : neighbours) {
                        if(graphAdapter.isAvailable(neighbour.position)) {
                            typename std::set<NodeAdapterType>::iterator cIter, oIter;

                            cIter = closed.find(neighbour);
                            if(cIter != closed.end()) {
                                continue;
                            }

                            neighbour.g(current->g() + 1);
                            neighbour.h(graphAdapter.getHeuristicCostLeft(neighbour, neighbour));

                            oIter = open.find(neighbour);
                            if(oIter == open.end() || neighbour.g() < oIter->g()) {
                                if(oIter != open.end()) {
                                    open.erase(oIter);
                                }

                                auto cameFromIter = came_from.find(neighbour);
                                if(cameFromIter != came_from.end()) {
                                    if(cameFromIter->second.g() > neighbour.g()) {
                                        came_from.erase(cameFromIter);
                                    }
                                }
                                came_from.emplace(std::pair<NodeAdapterType, NodeAdapterType>(neighbour, *current));

                                open.insert(neighbour);
                            }
                        }
                    }

                    open.erase(current);
                }

                // If algorithm comes here, no path was found, and return value of this method will be empty vector

                return resultPath;
            }
开发者ID:RippeR37,项目名称:AI-2015-Vindinium-bots,代码行数:72,代码来源:AStar.hpp


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