Ideas The maze problem is essentially a shortest path problem. bfs+堆(先广搜索)It is generally used but not used for the shortest path problem dfs. Because dfs needs to backtrack repeatedly, the time complexity is very high, and bfseach node only needs to be traversed once, and the time complexity is O(n)(not counting heap sorting).
The principle of bfs to find the shortest path The idea of bfs to find the shortest path is to find the shortest distance from the current node to the starting point in each round of traversal. If it is found that the shortest distance of the node has been found, then there is no need to traverse this node. Since bfs+堆the weight of each node is the shortest distance from the starting point to the node when it is traversed for the first time, in fact, each node only needs to be traversed once for a graph. bfs+堆The fact that the distance between each traversed node is guaranteed to be the smallest is based on the following fact: the shortest distance from the starting point to node i = min (the shortest distance from the starting point to the adjacent point of i) + 1 the shortest distance from the starting point to node i = min( The shortest distance from the starting point to the adjacent point of i)+1The shortest distance from the origin to node i=min ( the shortest distance from the starting point to the adjacent point of i )+1