繁体   English   中英

无法创建由“父级”链接的元素列表

[英]Can't create a list of elements linked by a “Parent”

我正在尝试创建一种方法(使用* 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);
                }
            }
        }
    }
}

(..)

类Node就是这样

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;
}

之后...在解决方法上..有这部分代码

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

newPath.SetParent(t);

我认为这是错误,这是newPath,它指向t时应该指向打开的列表中的节点

是真的吗 如果是..我该如何解决?

您的代码量需要更深入的调查,但是基本上我发现您的方式不正确。 假设n是您的最后一个节点。 然后像这样回来

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

执行此操作后,n将是初始节点

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM