I have a recursive maze solver algorithm that can successfully work its way through the maze. The only problem is that I can not find a way to save the shortest path between the starting point and the ending point. How would I save the coordinates of the shortest path?
This is the recursive function
void Solve_Maze(int coorx,int coory) {
if((coorx>=0)&&(coorx<l)&&(coory>=0)&&(coory<w)) {
if((Map[coorx][coory]==Start)||(Map[coorx][coory]==path)) {
Map[coorx][coory]=visited;
Solve_Maze(coorx+1,coory);
Solve_Maze(coorx-1,coory);
Solve_Maze(coorx,coory+1);
Solve_Maze(coorx,coory-1);
}else if(Map[coorx][coory]==End) {
delete Map;
Solved=true;
}
}
}
After adding a vector to store the coordinates I got this output
(1,2)
(2,2)
(3,2)
(4,2)
(5,2)
(6,2)
(7,2)
(8,2)
(9,2)
(7,3)
(7,4)
(7,5)
(7,6)
(8,6)
(8,7)
(9,7)
(10,7)
It stores all of the coordinates but it even stores the coordinates of the path we do not want to take ((7,2)(8,2)(9,2) and then back to (7,3)). Is there a way that I can store just store the shortest path?
Here’s how you can keep track of the solution coordinates using a vector:
Output:
Also note that my
Map[][]has reversed coordinates.