BFS+ Priority Queue - Maze Shortest Path - Comparison and Detailed Diagram of Two Optimal Methods

http://blog.csdn.net/qq_36523667/article/details/78638354

There is a maze in this link. BFS+ priority queue is used.


I've been wondering where is the priority queue? I feel that it is no different from direct bfs? Later, it was proved that the approach was different and the thinking was different.

So here is another best practice.


normal bfs


Black is the current point, and yellow is the point to be explored. Put the yellow ones into the queue in turn, and then out of the queue, one at a time.

Suppose you go out first to the point on the right.

Then the right of the point on the right, the bottom will go to the end of the queue. Then put out the second yellow point, and the point below the second yellow will enter the queue (because the point on the right has already entered), add it to the end, and exit in turn.


Priority queue + BFS


(Assuming that 3 means that the last entry has to wait for 3 seconds, which is an unfriendly situation) No matter which of the yellow dot below us and the yellow dot 3 is the first to enter the queue, 3 will be processed by the internal mechanism of the priority queue at the end of the queue. .

So when the first yellow dot is out of the queue, the right side of the first yellow dot will enter the queue again.

However, according to the mechanism of the priority queue, the two newly entered points will run to the front of 3 again.

Therefore, our approach is to avoid time-consuming operations, and let the time-consuming or unfriendly points dequeue at the end, which may allow us to reach the end faster.



And my approach.



I use a group departure mode, as in the first picture, the two yellow areas we are about to visit and arrive at the same time.

Set the original point to gray, which means it has been passed, use all the black points as the current point, and then find all the points that will be visited.

In this way, it spreads out like a virus step by step, even if it encounters an unfriendly and low priority point such as the situation 3 mentioned in the above example,

I do the same, but it will stay at the 3 point for 3 seconds.

In this way, all possible situations are covered.



The above bfs + priority queue and my method, can't say which is better. The two methods are suitable for different types of questions.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324078524&siteId=291194637