. *** * |
***** * *** *** |
* * |
* * * ** **O** |
* * * * |
********** *** |
* * **|
** * ** * |
你好家伙,我刚开始学习递归,我有点困惑,我的意思是,我有这个8x16迷宫(忽略右侧的酒吧)的起始位置迷宫是0,0,你可以在迷宫里出来,我从那里得到在C++中的递归的方式,“O”点,到目前为止,我已经尝试过这种了解递归C++
bool Laberinto::findPath(int x,int y){
//outside limits bounds
if(x < 0 || x > nRows || y < 0 || y > nCol)
return false;
//if goal
if(x == goalRow && y == goalCol){
updateLab();
return true;
}
//if obstacle
if(matrix[x][y] == '*' || matrix[x][y] == 'X'){
return false;
}
matrix[x][y] = '.';
updateLab();
//North checking
if(findPath(x-1,y) == true){
return true;
}
//East checking
if(findPath(x,y+1) == true){
return true;
}
//South checking
if(findPath(x+1,y) == true){
return true;
}
//West checking
if(findPath(x,y-1) == true){
return true;
}
matrix[x][y] = 'X';
updateLab();
return false;
}
updateLab()只打印迷宫,东西是它到了东边直到它找到了一个障碍,然后一直往南走,直到它崩溃,但是因为我刚刚开始学习递归,所以我没有看到我的错误发生在哪里
........*** * |
***** *.*** *** |
* * |
* * * ** **O** |
* * * * |
********** *** |
* * **|
** * ** * |
'goalCol'和'goalRow'从哪里来? – Michael
通过在调试器中运行以捕获实际崩溃并了解代码发生的位置。 –
你的问题不是递归,这是相当微不足道的,我建议你的关注你把注意力集中在错误的事情上。只需调试你的代码。由于递归,你很可能会崩溃。 – CarlH