简体   繁体   English

C中迷宫求解器的递归回溯算法不是回溯而是卡住

[英]Recursive backtracking alogrithm for a maze solver in C is not back tracking and gets stuck

I am trying to write a program in C that tracks the entire search process of a recursive backtracking algorithm of a maze solver.我正在尝试用 C 编写一个程序来跟踪迷宫求解器的递归回溯算法的整个搜索过程。 Thus, I'll be printing out every step taken.因此,我将打印出所采取的每一步。 The maze is a simple one with '#' representing walls, '.'迷宫是一个简单的迷宫,“#”代表墙壁,“.” representing empty cells and '@' representing the initial starting point.代表空单元格,“@”代表初始起点。 Given a starting point, we have to reach any of the edges of the maze to complete it.给定一个起点,我们必须到达迷宫的任何边缘才能完成它。 My search algorithm begins with trying to search for a path north, then east, then south then west.我的搜索算法首先尝试搜索一条向北、向东、向南、然后向西的路径。 (I used this for reference: http://www.cs.bu.edu/teaching/alg/maze/ ) (我用这个作为参考: http : //www.cs.bu.edu/teaching/alg/maze/

Example of an input of a given maze:给定迷宫的输入示例:

#######
###.###
###.###
...@..#
###.###
###.###
#######

What my code is trying to do / output:我的代码试图做什么/输出:

//step 1
#######
###.###
###.###
...+..#
###.###
###.###
#######

//step 2
#######
###.###
###+###
...+..#
###.###
###.###
#######

//Step 3
#######
###+###
###+###
...+..#
###.###
###.###
#######

//Step 4
#######
###X###
###+###
...+..#
###.###
###.###
#######

//step 5
#######
###X###
###X###
...+..#
###.###
###.###
#######

//Step 6
#######
###X###
###X###
...++.#
###.###
###.###
#######
.
. subsequent steps leading to final output
.
.
#######
###X###
###X###
++++XX#
###X###
###X###
#######

Here is a sample of the relevant functions from my code trying to execute this:这是我尝试执行此代码的相关函数的示例:

bool is_goal(long row, long col, long m, long n)
{
    if (row == 0 || col == 0 || row == m - 1 || col == n - 1) {
        return true;
    }
    return false;
}
 
bool is_wall(char **maze, long row, long col)
{
   if (maze[row][col] == WALL) {
       return true;
   }
   return false;
}
 
bool find_path(char **maze, char **maze_cpy, long row, long col, long m, long n, long     step)
{
    print_maze(maze_cpy, m, step);
    step += 1;
 
    //Base cases
    if (is_goal(row, col, m, n)) {
        return true;
    }
    if (is_wall(maze, row, col)) {
        return false;
    }
    //used to indicate current cell is part of soln
    maze_cpy[row][col] = '+';
 
    //Recursive cases
    //Go up
    if (find_path(maze, maze_cpy, row - 1, col, m, n, step)) {
        return true;
    }
    //Go right
    if (find_path(maze, maze_cpy, row, col + 1, m, n, step)) {
        return true;
    }
    //Go down
    if (find_path(maze, maze_cpy, row + 1, col, m, n, step)) {
        return true;
    }
    //Go left
    if (find_path(maze, maze_cpy, row, col - 1, m, n, step)) {
        return true;
    }
    //At this point, no solution
    maze_cpy[row][col] = 'X';
    return false;
}

However, my output recursive function just stops at step 3 and I can't figure out why it does not backtrack and update the path it has taken as an unusable path (with an 'X').但是,我的输出递归函数只是在第 3 步停止,我无法弄清楚为什么它不回溯并更新它作为不可用路径(带有“X”)的路径。 Help will be greatly appreciated.帮助将不胜感激。 (Sorry for the really long question, I tried to format it as nicely as possible). (对不起,这个问题太长了,我试图尽可能好地格式化它)。

There are more characters that find_path needs to stop (return false) for than just a WALL . find_path需要停止(返回 false)的字符不仅仅是WALL In the initial case, once it hits that top wall, it will look right and sees a wall, then looks down and sees a '+' , which is not a wall so it continues by starting a new search.在最初的情况下,一旦它碰到顶墙,它会向右看并看到一堵墙,然后向下看并看到一个'+' ,这不是一堵墙,因此它会继续开始新的搜索。 You end up bouncing between those two squares in the maze.你最终会在迷宫中的这两个方块之间弹跳。

find_path needs to immediately return false if the moved to is not a floor.如果移动到不是楼层,则find_path需要立即返回 false。 Something is not a floor if it is a WALL, part of the solution, or not a solution.如果某物是墙、解决方案的一部分或不是解决方案,那么它就不是地板。 Alternatively, check for floor characters (which seem to be just the '.' ).或者,检查地板字符(似乎只是'.' )。

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

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