I am trying to write a program in C that tracks the entire search process of a recursive backtracking algorithm of a maze solver. 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/)
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'). Help will be greatly appreciated. (Sorry for the really long question, I tried to format it as nicely as possible).