我正在尝试创建一种方法(使用 *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);
我认为它在这部分的错误,它指向t当它应该指向打开的列表上的节点时
这是真的吗?如果是..我该如何解决这个问题?