I traverse a 16×16 maze using my own A* implementation.
All is well. However, after the traversal, I would like to find out what wall would give me the best alternative path. Apart from removing every block and re-running A* on the maze, what’s a more clever and elegant solution?
I was thinking give every wall node (ignored by A*), a tentative F-value, and change the node structure to also have a n-sized list of node *tentative_parent where n is the number of walls in the maze. Could this be viable?
When you add a node to the list of nodes to consider, also add a flag whether the path through that node has already gone through a wall.
Then when considering possible paths from a node, if it hasn’t gone through a wall, consider adjacent walls to be fair paths.
You already have to maintain distance from the start node to the current node being processed, and it uses what you already have in place.
Full proof of concept:
We see that
operator==is defined to also consider whether or not the path has hit a wall yet. This allows us to consider the a node twice if needed, when we look in the closed set to see if we have already encountered this node. This is the case in the center hallway in the example maze in the source below.The portions of code marked with
#ifdef I_AM_ALLOWED_TO_GO_THROUGH_A_WALLshow the portions needed to augment a normal A* algorithm to be able to go through a single wall.