【问题标题】:Can't create a list of elements linked by a "Parent"无法创建由“父级”链接的元素列表
【发布时间】:2011-01-23 09:31:17
【问题描述】:

我正在尝试创建一种方法(使用 *A** 算法)来解决难题并将步骤返回到该解决方案。解决方案很简单..但我无法返回路径。

我使用了一个节点列表,然后每次推回一个新节点时,我都会将父节点设置为指向新节点的父节点;

list<Node> opened;
list<Node> closed;

Node current;

opened.push_back(start);

while( opened.size() !=0 )
{
   current = getLowestCostPath(opened);


   if(IsSolution(current) == true)
       return opened;

   opened.remove(current);

   if( !Has(closed, current))
          closed.push_back(current);


   for( int i = 0; i < static_cast<int>(current.GetBoard().capacity()); i++ ) 
   {   
   xx = current.GetBoard()[i].X();
   yy = current.GetBoard()[i].Y();

       for(int j = 0; j < static_cast<int>(current.GetBoard().capacity()); j++) 
       {
            if( isMovable(current))
            {
                //if found a new node
                Node newNode = Node(newBoard);
                Node *t = &current;

                if(!Has(opened, newNode ) && !Has(closed, newNode ) )
                {
                   newNode.SetParent(t);
                   opened.push_back(newPath);
                }
            }
        }
    }
}

(..)

类节点就是这样

class Node{

public:
   std::vector<Point> board;
   Path *parent;

   Node();
   Node(std::vector<Point> board)

   virtual std::vector<Point> GetBoard() const;
   virtual Path* GetParent() const;
   virtual void SetParent(Node *p); 

Node::Node(): board(),parent(NULL)
{
}

std::vector<Point> Node::GetBoard() const
{
return board;
}

void Path::SetParent(Node *p)
{
    this->parent = p;
}

Path* Path::GetParent() const
{
    return parent;
}

但后来我意识到我无法建立解决难题的路径......

我什至看不到一个父板...

for( list<Node>::iterator it = goal.begin(); it != goal.end(); it++)
{
    if( (*it).GetParent() != NULL ){
        cout << "writing a board" << endl;
        mas::WriteLine( (*it).GetParent()->GetBoard() , "\n");
    }
}

我搜索了整个互联网,但我无法意识到我做错了什么:(


我也试过了,但它会导致 VS2005 崩溃。

//目标是Solve方法返回的打开列表....

for(std::list<Node>::iterator it = goal.end(); it->GetParent() != NULL; t->GetParent())
{

    std::cout << "step" << std::endl;
    mas::WriteLine((*it).GetBoard(), "\n");
}

我正在尝试更加清晰和客观。所以...看这部分

current = getLowestCostPath(opened);

getLowestCostPath 方法返回的值来自:

Node Solution::getLowestCostPath( std::list<Node> l)
{
    std::list<Node>::reverse_iterator min = l.rbegin();
    int value = 0;
    for(std::list<Node>::reverse_iterator it = l.rbegin(); it != l.rend(); it++)
    {
        if( value < (*it).GetStep())
        {   
            min = it;
            value = (*it).GetStep();
        }
    }
    return *min;
}

在那之后...关于方法Solve..有这部分代码

//if found a new node
Node newNode = Node(newBoard);
Node *t = &current;

newPath.SetParent(t);

我认为这部分的错误,newPath 它指向 t,而它应该指向列表中的节点 opened

这是真的吗?如果是..我该如何解决这个问题?

【问题讨论】:

    标签: c++ pointers list iterator a-star


    【解决方案1】:

    您的代码量需要更深入的调查,但基本上我看到您错误地追溯了自己的方式。假设 n 是您的最终节点。然后像这样返回

    for (; n->GetParent() != NULL; n = n->GetParent())
      //do something on each node
    

    在这个执行之后,n 将是初始节点

    【讨论】:

    • 我试过这个...但它使 VS2005 崩溃。 for(std::list::iterator it = goal.end(); it->GetParent() != NULL; it->GetParent()) { std::cout
    • @DanceDisaster: list::end() 指向列表最后一个元素后面的一个。取消引用它是未定义的行为。
    • @DanceDisaster:只需使用我编写的代码并复制粘贴错误。
    • std::list::iterator it = --goal.end(); mas::WriteLine(it->GetBoard(), "\n");使用此代码我可以打印最后一个节点,但我说它->GetParent() 它是 NULL...所以 Parent 的集合是错误的!
    • 我认为它指向当前...当它应该指向列表中的元素时(它被分配到哪个当前)...这是真的吗?我需要一点帮助,拜托!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-09
    • 1970-01-01
    • 2017-08-04
    • 2018-07-25
    • 1970-01-01
    • 2020-05-05
    • 2022-06-23
    相关资源
    最近更新 更多