无法创建由“父级”链接的元素列表
我正在尝试创建一种方法(使用 *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 = ¤t;
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 = ¤t;
newPath.SetParent(t);
我认为错误就在这部分上,newPath 它指向 t,而它应该指向列表上的节点 opened
这是真的吗?如果是……我该如何解决这个问题?
I'm trying to create a method (using the *A** algorithm) that solves a puzzle and returns the steps to that solution. 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 = ¤t;
if(!Has(opened, newNode ) && !Has(closed, newNode ) )
{
newNode.SetParent(t);
opened.push_back(newPath);
}
}
}
}
}
(..)
the class Node it's just this
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.
//goal it's the opened list that the method Solve returned....
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:
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 = ¤t;
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
Is it true? If it is.. how can I fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码量需要更深入的调查,但基本上我看到您错误地追溯了您的方式。假设 n 是您的最终节点。 像这样返回n 将是初始节点
执行完后
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. then return back like this
after this executes n will be the initial node