Consider using a two-dimensional array to represent the maze. All the pathways used in 0, the wall with 1, the export is 9, the inlet is 6, and has been indicated by 3 points. Output out of the maze process.
From the process of solving this problem, we can sum up two algorithms, one is the exploring process and the other is the output route.
1. The Pathfinder Process
The Pathfinder algorithm can be summed up as:
[1] Starting from the entrance position, check the four directions in the east-west path, if found that exit is successful exit, otherwise all the access coordinates into the stack;
[2] Take a coordinate from the stack, mark it as the current position (Mark number 3), and judge the access situation again;
[3] This is done until the exit is found to exit successfully, if the stack empty and did not find the exit, then failed to exit.
The backtracking procedure used here can be described as:
At each point, all possible path coordinates (the node labeled number 0) are pushed into the stack. So when you reach a point where there is no possible pathway, naturally there is no corresponding coordinate pressure into the stack, and at this point from the stack to take out a point on the pressure of a possible path to the coordinates, and continue to make the path to judge, this is a retrospective process .
2. Output a shorter route
All the points passing through the path (Mark number 3 node) are stored in the queue according to the actual Pathfinder route, the enemy is the entrance, and the tail of the team is exported. These points may have a detour, so you can use the following algorithm to output a shorter route.
[1] Set the team tail (exit) node as the current judgment node;
[2] Starting from the predecessor node of the Current Judgment node (x,y), traversing the queue forward, if the adjacent node (whose coordinates can be (x+1,y), (X-1,y), (x,y+1), (x,y-1) is found, deletes the node to all nodes between the predecessor nodes of the current judgment node;
[3] The Phase Pro node is set as the current judgment node and the phase Pro node is continued to be judged;
[4] exits when the current Judgment node is the team head node.
The route that the algorithm obtains is not necessarily the shortest route, want to get the shortest route, consider using the tree structure to keep all the routes from the exit to the entrance to a subtree, and the shortest tree of trees is the minimum route. However, this algorithm guarantees that the resulting route will not have a detour.