1. 主要思想
循环前,generate root,入队;
循环中, 队列出队并作为root,产生childs,逐个node验证是否目标,不是则push入队。
2. 代码示例
<code:C++>
class Palace;
classNode
{
virtual bool IsTarget()const;
virtual Node* NextChild(void*);
virtual bool operator < (const Node& node)const;
virtual bool operator == (const Node& node)const;
static Palace* pPalace_;
};
Node* Node::NextChild(void* )
{
……
if (HasChildNode())
return pPalace->AddNode(ChildNode);
else
return 0;
}
class Palace
{
public:
set<Node> nodes;
queue<Node*> pNodes;
Node* AddNode(const Node& node);
Node* bfs_search(void* );
}
Node* Palace::bfs_search(void* )
{
Node * pNode;
Node* pChild = 0;
Node root(source_state);//source_state from void*
pNodes.push(&(*nodes.insert(root).first));
while (pNodes.size() < MAX_NODE_COUNT)
{
assert(pNode = pNodes.front());
for (each eligible arg )//arg from void*
{
if (pChild = pNode->NextChild(arg))
{
pNodes.push(pChild);
if (pChild->IsTarget(target_state))//target_state from void*
{
return pChild;
}
}
}
pNodes.pop();
}
}
</code>
posted on 2007-06-09 20:38
Tempwmk 阅读(448)
评论(0) 编辑 收藏 引用