Queue hierarchy -- thought of by the breadth-first search method

Source: Internet
Author: User

Yesterday, I considered a trojan checkers program. The idea was very simple: I used the breadth-first search method for searching. However, when I thought about it, I solved the problem of a queue processing hierarchy, once there was a problem similar to the use of the queue layer. At that time, I didn't think about it carefully, and the problem fell down.

Original question:

In an 8*8 chess board, enter the start coordinate and end coordinate of the horse. How many steps do the horse need to jump from the start position to the end position?

For example, when a horse jumps from A1 to E4, the breadth-first search method is used to mark the points that can be jumped every time.

The point that can be jumped to in the first step

The point that can be jumped to in step 2

The point that can be jumped to in step 3 (you can already jump to the destination E4)

The core idea of the program is the breadth-first search mentioned above. It takes three steps to use the queue to find a Trojan, that is to say, there are three queue layers (marked as 1, 2, and 3 respectively in the figure): for the first time, A1 is the center and testing is performed in eight directions, determine that B3 and C2 meet the requirements (that is, the coordinates are in the checkerboard). The second test is centered on B3 and C2, respectively, and the points that meet the requirements are obtained; the third search continues based on the second result and finds the points that meet the requirements. Because all vertices are placed in the queue, we do not know whether to search for the vertices obtained during the first search. As a result, no result is obtained when we finally find the desired steps. My idea is to set a variable location to mark the subscript of the last vertex of a level in the queue to record the queue level, check whether a vertex is the last vertex of a level before each vertex search. If yes, after the vertex search is complete, the location is updated to record the subscript of the last vertex of a new level.

The following is a pseudo code:

While (! Empty (Queue ))
If (location = queue. Head) // the current search is the last of a queue.
Flag = true;
// Skip the intermediate search code
Location = queue. rear; // change location to the subscript of the last element in the new layer.
Flag = false;
Level ++; // level 1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.