简体   繁体   English

使用递归来解决C ++中的迷宫?

[英]Using recursion to solve mazes in C++?

I'm trying to create a program that can solve mazes through recursion. 我正在尝试创建一个可以通过递归解决迷宫的程序。 I'm basing my code on a few steps that can be found online, specifically: 我的代码基于可以在网上找到的几个步骤,具体来说:

  1. if (x,y outside maze) return false if(x,y outside maze)返回false
  2. if (x,y is goal) return true if(x,y是goal)返回true
  3. if (x,y not open) return false if(x,y not open)返回false
  4. mark x,y as part of solution path 标记x,y作为解决方案路径的一部分
  5. if (FIND-PATH(North of x,y) == true) return true if(FIND-PATH(x,y之前)== true)返回true
  6. if (FIND-PATH(East of x,y) == true) return true if(FIND-PATH(东经x,y)== true)返回true
  7. if (FIND-PATH(South of x,y) == true) return true if(FIND-PATH(x,y之后)== true)返回true
  8. if (FIND-PATH(West of x,y) == true) return true if(FIND-PATH(西x,y)== true)返回true
  9. unmark x,y as part of solution path 取消标记x,y作为解决方案路径的一部分
  10. return false 返回false

I have seen at least two other questions with this algorithm, but I'm pretty sure the problems weren't exactly the same. 我已经看到这个算法至少有两个问题,但我很确定问题并不完全相同。

bool path (string maze[], int x, int y){
    values val;
    bool check;
    //for (int k=0; k<val.xDim; k++) cout<<maze[k]<<endl;
    cout<<x<<":"<<y<<endl;
    if (x>val.xDim || y>val.yDim || x<0 || y<0) {cout<<"end\n"; return false;  }
    if (maze[x][y]=='x') return true;                           //If exit is reached
    if (maze[x][y]=='%' || maze[x][y]=='+') return false;       //If space is filled
    maze[x][y]='+';
    if (path(maze, x-1, y)==true) return true;
    cout<<"endtwo\n";
    if (check=path(maze, x, y+1)==true) return true;
    if (path(maze, x+1, y)==true) return true;
    if (path(maze, x, y-1)==true) return true;
    maze[x][y]='.';
    return false;
}

int main(){
    if (path(maze, val.startX-1, val.startY)==true) {
        for (int k=0; k<val.xDim; k++) cout<<maze[k]<<endl;
    } else cout<<"No solution found.\n";
}

The sample maze is (where e is the entrace and x is the exit): 样本迷宫是(其中e是入口,x是出口):

%e%%%%%%%%%
%...%.%...%
%.%.%.%.%%%
%.%.......%
%.%%%%.%%.%
%.%.....%.%
%%%%%%%%%x%

Output: 输出:

-1:1
end
No solution found.

From what I can tell, the path method should begin by checking the space directly above the entrance, which is out of the maze (returning false). 从我所知道的,路径方法应该首先检查入口正上方的空间,该空间位于迷宫之外(返回false)。 Following this, it should check east (and so on). 在此之后,它应该检查东(等等)。 However, when I run it, the function returns false and fails to continue onto the following if statements. 但是,当我运行它时,该函数返回false并且无法继续执行以下if语句。 This is shown by the fact that "end" is printed, while "endtwo" (found after the north check) is not. 打印“end”的事实表明了这一点,而“endtwo”(在北检查后发现)则没有。 I'm not sure if there's some form of problem with my recursion logic or my implementation of recursion, so I'm hoping on some clarification on this. 我不确定我的递归逻辑或递归实现是否存在某种形式的问题,所以我希望对此有所澄清。

Thanks in advance! 提前致谢!

Your first check in bool path(...) finds x<0 since x==-1, so the function returns false and exits, and the main program gets a false result from the call to path , prints what he has to and exits. 你的第一次检查bool path(...)发现x <0,因为x == - 1,所以函数返回false并退出,主程序从path调用得到一个false结果,打印他必须的和退出。

You should start your checks with a valid position. 你应该用有效的职位开始你的支票。

You are starting from an invalid position, So instead of this if (path(maze, val.startX-1, val.startY)==true) { , try this if (path(maze, val.startX, val.startY)==true) { . 你是从一个无效的位置开始的,所以而不是这个if (path(maze, val.startX-1, val.startY)==true) { ,试试这个if (path(maze, val.startX, val.startY)==true) { Actual recursion part seem alright to me, provided that you do not mind replacing 'e' from the maze with a '.' 实际的递归部分对我来说似乎没问题,前提是你不介意用'.'替换迷宫中的'e' '.' .

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

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