首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C++ A*算法查找成员值在<deque> openlist中最低的结构实例

C++ A*算法查找成员值在<deque> openlist中最低的结构实例
EN

Stack Overflow用户
提问于 2016-01-04 15:30:18
回答 1查看 183关注 0票数 0

你得原谅我,我的语法技巧不太好。我目前正在尝试使用一个AStar方法创建一个unique_ptr算法,为了匹配这个方法的伪代码,我需要能够在我的开放列表中找到一个具有最低f_cost的对象,但是我无法理解语法。

您需要了解的网格是一个10x10网格,其中有一个模型数组,该数组也是链接到结构的10x10。

该结构由未在此提供的某些布尔值组成,因此我可以计算f_cost,即启发式成本+曼哈顿距离。

代码语言:javascript
复制
struct node
{
//Code by RRNewell Uclan 20618255 -- I have to put this here because my 
//university uses refworks, so this is to confirm on submission it is my work. 

 public:
 int Loc_X;
 int Loc_y;

 int manhattandistance; 
 int f_cost

};

void AStarFindPath(IModel* NodeCubeModels[10][10], node NodeArray[10][10], int storeStartLoc_X, int storeStartLoc_y, int storedestLoc_x, int storedestLoc_y)
{
    deque <unique_ptr < node > > openList; 
    deque <unique_ptr < node > > closeList;  
    deque <unique_ptr <node> >::iterator p;

    unique_ptr <node> currentNode(new node); 
    unique_ptr <node> tmpNode(new node); 



    findstartnode(ANodeCubeModels, SNodeArray); 
    tmpNode->Loc_X = storeStartLoc_X; 
    tmpNode->Loc_y = storeStartLoc_y; 
    cout << "Pushing start node onto list, Node:" << storeLoc_X << ":" << storeLoc_y << endl;

    openList.push_back(move(tmpNode));


    while (currentNode->Loc_X != storedestLoc_x && currentNode->Loc_y != storedestLoc_y)
    {
        deque<node>min_element(openList.begin(), openList.end());<<--- <<HERE>>
        // Is where I want to find node on the open list with the lowest f_cost
    }
}

<<Here>> (在标记deque<node>min_element(openList.begin(), openList.end());的代码上标记)是我希望在打开的列表中找到f_cost最低的节点的地方。

有人知道这个的句法操作吗?或者如果可能的话,还是我必须换一种方式?

谢谢你的帮助。

编辑伪码图像

我正试图按照sebastian lague高亮显示的第5步/第5行的伪代码进行操作。

编辑的原因:稍作澄清。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-04 18:30:03

min_elementstd命名空间中的一个方法。所以:

代码语言:javascript
复制
deque <unique_ptr <node> >::iterator current_it = 
                                        std::min_element(openList.begin(), openList.end());
std::unique_ptr<node> current = std::move(*current_it);

注意,您的node结构应该实现一个operator<,或者应该提供一个比较器。后者可能更干净,因为比较是特定于算法的:

代码语言:javascript
复制
auto current_it = 
    std::min_element(openList.begin(), openList.end(), 
        [&](const std::unique_ptr < node > & lhs, const std::unique_ptr < node > & rhs) 
        {
            return lhs->f_cost < rhs->f_cost; 
        });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34594507

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档