简体   繁体   English

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

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

I'm trying to create a method (using the *A** algorithm) that solves a puzzle and returns the steps to that solution. 我正在尝试创建一种方法(使用* A **算法)来解决难题并将步骤返回至该解决方案。 The solution it's easy.. but I can't return the path to that. 解决方案很简单..但是我无法返回该路径。

I used a list of Nodes and then every time a push back a new Node I set the parent pointing to the Node which new came; 我使用节点列表,然后每次推回新节点时,都会将父节点设置为指向新节点的节点。

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

(..)

the class Node it's just this 类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;
}

But then I realized that I can't BUILD the path to solve the puzzle... 但是后来我意识到我无法建立解决难题的途径...

I can't even see a single Parent board... 我什至看不到一个家长委员会...

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

I've searched all over the internet and I can't realize what am I doing wrong :( 我已经在整个互联网上搜索了,但我不知道我在做什么错:(


I also tried this but it makes VS2005 Crash. 我也尝试过,但这会使VS2005崩溃。

//goal it's the opened list that the method Solve returned.... //目标是Solve方法返回的打开列表。

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

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

I'm trying to be more clear and objective. 我试图变得更加清晰和客观。 So... see this parts 所以...看这部分

current = getLowestCostPath(opened);

The value returned by the method getLowestCostPath came from: 方法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;
}

after that... on the method Solve.. there is this part of the code 之后...在解决方法上..有这部分代码

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

newPath.SetParent(t);

I think that the ERROR it's on this part, newPath it's pointing to t when it should be pointing to node on the list opened 我认为这是错误,这是newPath,它指向t时应该指向打开的列表中的节点

Is it true? 是真的吗 If it is.. how can I fix this? 如果是..我该如何解决?

your volume of code require deeper investigation, but basically what i see that you trace back your way incorrectly. 您的代码量需要更深入的调查,但是基本上我发现您的方式不正确。 assume n is your final node. 假设n是您的最后一个节点。 then return back like this 然后像这样回来

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

after this executes n will be the initial node 执行此操作后,n将是初始节点

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

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