Using VB to solve Huarong road problem

Source: Internet
Author: User
Tags final
Question 1, the question of the proposed
1.1 "Huarong Road" Introduction
"Huarong Road" is a kind of ancient Chinese intellectual game toys, in a rectangular box with a width of 4 and a length of 5, there are 10 pieces, including a Caocao, a five-Tiger admiral, and four pawns, requiring that the pieces be moved without overlapping, most of which will move the devil from the top of the chessboard to the bottom of the Center for success. As the five generals can be placed horizontally can also be vertical, there are many kinds of arrangement, so that can form a very complex chess game, people also played a lot of popular chess name, such as the next picture is "horizontal knife immediately" layout.

Fig. 1.1 The layout of "horizontal knife immediately"

The solution to this problem, now has a lot of articles in detail, this article is no longer one by one description, specifically for the "Breadth first algorithm" way to solve the discussion.
Discussion on 1.2 breadth-first algorithm
We know that the problems like "Huarong", such as maze problem, can be summed up as the traversal and shortest path problem of graph in graph theory. That is to say, starting from a specific state as the starting point of the diagram, every step out of the state as the extension of this node, as long as the state of the exit is not repeated in the past, you can traverse all of the original state may arrive all States, Thus forming a complete inverted-position tree diagram, as shown in the following figure.

Fig. 1.2 Schematic diagram of State transformation

To solve the problem of "Huarong Road", the essence lies in how to construct such a state-tree graph quickly and to find the final result quickly when the data quantity is unknown. To do this, there are many algorithms, breadth first is one of the more popular algorithm. Its specific ideas are:
Starting from the beginning, first build the first layer of nodes, and then build the second layer, the third layer of nodes, in the process of construction, in order to ensure that the data does not recur, each new node needs to be compared with all the original generated nodes, to ensure that they do not recur in the diagram.
The problem arises because, each time a new node is added, all need to compare with all the original nodes to ensure that this node does not recur, as the number of nodes increased, each need to be more and more, so the need for a lot of time to compare the state is repeated, thus creating a bottleneck in the efficiency of the algorithm. This is also one of the important reasons that the breadth-first algorithm is inefficient.
The following discusses how to optimize the breadth-first algorithm to improve efficiency.
2. Optimization of breadth-first algorithm
We now assume that we have found a shortest path from the starting point State to the final result state, so we can obviously get the following inference:
From the starting point to each specific state on this shortest path, the path that is taken is the shortest path for this node state.
In other words, we have to find the shortest path from the beginning to the end, only by walking the shortest path of each node to get.
We are now giving each node in the graph a corresponding shortest path step, forming a node state tree diagram with path steps as shown below.

Fig. 2.1 A state tree diagram with shortest path steps


With the above diagram, we can easily get the following conclusion:

In the shortest path tree graph, nodes that are connected to any node of level n are bound to be between [n-1,n+1]. (Conclusion 1)

Then, starting from a node of level n, all the nodes that are obtained, their level can only be between (n-1,n+1). Therefore, the following conclusions are obtained:

To determine whether a node generated from a node of the original level n already exists in the diagram, it is only necessary to determine if there is a node in the node set at [n-1,n+1] level in the diagram, and if not, then the node is the new node. (Conclusion 2)

Note that conclusion 2 above, according to conclusion 2, in the question of whether the new generation node is repeated, it is only necessary for the node to trace back to the upper two poles, and not to go back to the first node.

In this way, we achieve the optimization of breadth-first algorithm by narrowing the range of judgment for the existence of the new generation node.

3. Conclusion

In the search process of breadth first algorithm, if searching according to the rule of shortest path, then for each new node state, we can judge the new node's state whether it is duplicated, so that the goal of fast searching is achieved. This optimization of breadth-first algorithm can also be used to solve other similar problems, such as "Eight Queens Problem", maze walking problem, shortest traffic route problem and so on.




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.