【发布时间】:2011-08-15 22:20:47
【问题描述】:
我已经阅读了很多类似的文章,如果这已经得到回答,我很抱歉,但我仍然卡住了。
我正在编写一个函数来填充一棵树,每个节点都有四个分支,它们将存储对“八块拼图”状态的可能操作,即http://www.8puzzle.com/images/8_puzzle_start_state_a.png
由于手头问题的严重递归性质,我认为问题是堆栈溢出。
尾递归似乎是解决方案,但我不确定它是否相关/可能或如何在这种情况下实现它。代码如下:
void Tree::insert(string &initialState, const string &goalState, tree_node *&inNode){
cout<<"insert called"<<endl;
depth++;
cout<<depth<<endl;
string modState;
int zeroPos=0;
if(initialState==goalState){
cout<<"* * * GOAL * * *"<<endl;
getchar();
exit(0);
}
if(inNode==NULL){//is this the first node?
inNode = new tree_node(initialState);
root=inNode;
inNode->parent=NULL;
insert(initialState, goalState, inNode);
}else{
inNode->state = initialState;
for(zeroPos=0;zeroPos<initialState.size();zeroPos++){//where is the empty tile?
if(initialState[zeroPos]=='0'){
break;
}
}
//left
if(zeroPos!=0 && zeroPos!=3 && zeroPos!=6){//can the empty tile move left?
modState=initialState;
modState[zeroPos]=modState[zeroPos-1];
modState[zeroPos-1]='0';
if(isOriginal(modState, inNode) ){//does this state already exist?
cout <<"left " << modState[0]<<modState[1]<<modState[2]<<endl;
cout <<"left " << modState[3]<<modState[4]<<modState[5]<<endl;
cout <<"left " << modState[6]<<modState[7]<<modState[8]<<endl;
inNode->l = new tree_node(modState);
inNode->l->parent= inNode;
if(inNode->l != NULL){
insert(modState, goalState, inNode->l);
}
}
}
}
}
}
【问题讨论】:
-
我没有检查过你的代码,但是如果你的算法工作正常,如果你在 8-puzzle 上遇到堆栈溢出,我会很惊讶。
-
也许所有状态的树不是最好的实现?
-
我可能应该提到我打算运行各种搜索算法,找到到达目标状态的步骤,并记录它们在各个方面(时间、内存使用等)的效率。
-
@RemnantXO 为什么要在运行搜索算法之前创建状态树?
-
嗯。是否有替代方法来运行它们?如果不将它们排列成某种结构,我想不出如何搜索这些状态。也许我只是非常愚蠢并且错过了显而易见的事情?
标签: c++ recursion tree stack-overflow