So I’d like to create a shortest path maze solver that solves a maze. The maze is similar to this:
My mazes will always have a wall around them. Also, the @s are walls if you couldn’t tell.
@@@@@@@@
@ S@ @
@@@ @@E@
@ @ @
@@@ @@
@@@@@@@@
Where S is the start and E is the end.
I’d like to apply Dijkstra’s algo, but I don’t understand how to actually implement it. It’s like:
- Check current position (which is start at the beginning). If it’s E, return “path” <– which is…? Else, mark current position as visited and somehow mark which position it came from…
- Enqueue all of current positions neighbors that aren’t walls and aren’t already visited.
- Repeat number 1 for all of the neighbor, and enqueue all the neighbors of the neighbors.
… I’m confused, please help. I have a class that holds the x and y coordinates of both start and finish, as well as a char[][] of the actual maze. Also, I’m attempting to print out the solved version of the maze. That is, replace the shortest path positions with something like a period. Any help is greatly appreciated.
One of the most widely used and effective pathfinding algorithms is the A* Pathfinding Algorithm Whilst this is probably surplus to your needs, it is a often used in games and other forms of computer science, and is scalable to your needs.