簡體   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