简体   繁体   English

3d迷宫递归方法 - c ++

[英]3d maze recursion method - c++

I am making a 3D maze in c++. 我正在用c ++制作一个3D迷宫。 I am having trouble with a recursive method to find a valid path between the two endpoints (starting point is m[0][0][0]; endpoint is m[7][7][7];). 我在使用递归方法找到两个端点之间的有效路径时遇到问题(起始点是m [0] [0] [0];端点是m [7] [7] [7];)。 It checks positions in the array. 它检查阵列中的位置。 If its contents are a 1, then it is a valid part of the path; 如果其内容为1,则它是路径的有效部分; if 0, it is not a valid part of the path. 如果为0,则它​​不是路径的有效部分。 Here is my method: 这是我的方法:

bool Maze::findPath(int row, int column, int level,string path){
cout << "findPath " << row << ", " << column << ", " << level << " value " << m[row][column][level] << endl;
if(row < 0 || row > 7 || column < 0 || column > 7 || level < 0 || level > 7 ){
    cout << "Out of bounds" << endl;
    //system("PAUSE");
    return false;
}
else if(m[row][column][level] == 0){
    cout << "spot is zero" << endl;
    //system("PAUSE");
    return false;
}
else if(visited[row][column][level] == 1){
    cout << "visited" << endl;
    return false;
}
else if(row == 7 && column == 7 && level == 7 && m[row][column][level] == 1){
    cout << "Found!" << endl;
    //system("PAUSE");
    return true;
}
else{
    visited[row][column][level] = 1;
    //cout << "searching..." << endl;
    if(row < 7 && findPath(row + 1,column,level,path))
        return true;
    if(column < 7 && findPath(row,column + 1,level,path))
        return true;
    if(level < 7 && findPath(row,column,level + 1,path))
        return true;
    if(row > 7 && findPath(row - 1,column,level,path))
        return true;
    if(column > 7 && findPath(row,column - 1,level,path))
        return true;
    if(level > 7 && findPath(row,column,level - 1,path))
        return true;
}
return false;

} }

So the method checks for "Out of bounds", an invalid spot on the path (zero), a visited location. 因此,该方法检查“越界”,路径上的无效点(零),访问位置。 I'm not sure what exactly I'm missing, but the method returns true to mazes that are unsolvable. 我不确定我到底错过了什么,但该方法将true返回到无法解决的迷宫。 Can anybody see some blatant mistake that I may be missing with my recursive call? 任何人都可以看到我的递归通话可能会遗漏的一些明显的错误吗? Thanks 谢谢

EDIT: Fixed a few code mistakes, but it still seems to be "solving" unsolvable mazes. 编辑:修正了一些代码错误,但它仍然似乎是“解决”无法解决的迷宫。

Here's an example of a solvable maze that it is saying is not possible to solve: 这是一个可解决的迷宫的例子,它说不可能解决:

1 0 0 0 0 0 0 1 
0 0 0 0 0 1 0 0 
0 0 0 0 0 0 0 0 
0 0 0 1 0 0 0 1 
0 0 0 1 0 0 0 0 
1 0 0 1 0 1 0 0 
0 0 0 1 0 0 0 0 
1 0 0 1 0 0 0 1 

1 0 0 0 0 0 0 0 
1 0 0 0 0 0 0 0 
1 1 1 1 0 0 0 0 
0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 
1 0 0 0 0 0 0 0 
0 1 1 0 0 0 0 0 
0 0 0 1 0 1 1 1 

0 0 0 0 0 0 0 1 
0 0 0 0 0 0 0 0 
0 0 0 1 0 0 0 1 
0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 
0 1 0 0 0 0 0 0 
0 0 0 0 0 0 0 1 
0 0 0 1 0 0 0 0 

0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 1 
0 0 0 1 0 0 0 0 
0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 
1 0 0 0 0 1 0 0 
0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 1 

1 1 1 1 0 0 0 0 
0 0 0 1 0 0 0 0 
0 0 0 1 0 0 0 1 
0 0 0 0 0 0 1 0 
0 0 0 0 0 0 1 0 
1 0 0 0 0 1 0 0 
0 1 0 0 0 0 0 0 
1 0 0 0 0 0 0 1 

1 0 0 0 0 0 0 0 
0 0 0 0 0 0 1 0 
0 0 0 0 0 0 1 0 
0 0 0 0 0 0 1 0 
1 1 1 1 0 0 0 0 
0 0 0 1 0 0 0 0 
0 0 0 1 0 0 0 0 
1 1 1 1 0 0 0 1 

1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 
0 0 1 0 0 0 0 0 
0 0 1 0 0 0 0 0 
0 0 1 0 0 0 0 0 
0 0 1 0 0 0 0 0 

0 0 1 0 0 0 0 1 
0 0 1 0 0 0 0 1 
0 0 1 0 0 0 0 1 
0 0 1 0 0 0 0 1 
0 0 1 1 0 0 0 1 
0 0 0 1 0 0 0 1 
0 0 0 1 0 0 0 1 
0 0 0 1 1 1 0 1 

There's a problem in the findPath(++row,column,level,path) (and similar recursive calls): you don't want the variable increments to carry over to the other recursive calls. findPath(++row,column,level,path) (以及类似的递归调用)存在问题:您不希望变量增量转移到其他递归调用。 (For example, the variable row in findPath(row,++column,level,path) would be affected by the first recursive call.) (例如, findPath(row,++column,level,path)的变量row findPath(row,++column,level,path)将受到第一次递归调用的影响。)

Use findPath(row + 1,column,level,path) (and similar) instead. 请改用findPath(row + 1,column,level,path) (和类似的)。

Also, in the last three recursive calls, you're not making the right tests: 此外,在最后三次递归调用中,您没有进行正确的测试:

//instead of level < 7
if(level < 7 && findPath(--row,column,level,path)) //should be row > 0
    return true;
if(level < 7 && findPath(row,--column,level,path)) //should be column > 0
    return true;
if(level < 7 && findPath(row,column,--level,path)) //should be level > 0
    return true;

EDIT 编辑

However, you don't actually need these tests since you filter out out of bounds errors at the top of your recursive function. 但是,您实际上并不需要这些测试,因为您在递归函数的顶部过滤掉out of bounds错误。 Therefore, these calls can be simplified to: 因此,这些调用可以简化为:

return  findPath(row + 1,column,level,path) || findPath(row,column + 1,level,path)
          || findPath(row,column,level + 1,path) || findPath(row - 1,column,level,path)
          || findPath(row,column - 1,level,path) || findPath(row,column,level - 1,path);

Additionally, the test && m[row][column][level] == 1 is redundant since the else if(m[row][column][level] == 0) takes care of that. 另外,测试&& m[row][column][level] == 1是多余的,因为else if(m[row][column][level] == 0)负责处理。 (I'd check m[7][7][7] before even calling this function the first time, by the way.) (顺便说一下,在第一次调用此函数之前,我会检查m[7][7][7] 。)

I just Finished this algorithm as an assignment for a class, ours only used a 5x5 block as the maze, but I found that it will go very slowly testing all possibilities each time it reaches the block from any angle, I found that the program can be sped up significantly by setting values in your array to 0 as you determine that they're not useful. 我刚刚完成了这个算法作为一个类的赋值,我们只使用了一个5x5块作为迷宫,但是我发现每次从任何角度到达块时都会非常缓慢地测试所有可能性,我发现程序可以通过将数组中的值设置为0来显着加速,因为您确定它们没用。 I did it at the return false at the end of the function. 我在函数结束时return false时完成了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM