I'm coding a maze solving algorithm and I'm having trouble with the backtracking once you hit a "wall"
So far I have that my code checks some basic recursive cases such as if I've reached the end or if I have checked all points. I've also made it so that there's a list that keeps track of the solution "path" and adds and removes points accordingly. So every time it adds a point, it checks if its valid and checks the point, above, to the left, to the right, and below etc. If none are valid then this statement is executed
else:
path.pop()
return solve_maze(maze,path,end)
this removes the point and backtracks and checks the following conditions at the beginning of the function.
square = list(path[-1])
#Base Case
if end in path:
return True
elif square == None:
return False
elif maze[square[0]][square[1]] == "X":
path.pop()
return solve_maze(maze,path,end)
elif tuple(square) in path:
path.pop()
return solve_maze(maze,path,end)
However when I execute the last line, it just removes all the points in my path and i get an indexing error.
Any advice on how I can backtrack once I hit a dead end?