Probably the hardest cat to surround--an algorithm for finding a winning path

Source: Internet
Author: User

The title borrowed from the old Luo style, haha (*^__^*)

Originally surrounded by the nerve cat game just fire, happened to be learning qml, handy to get a, do not know everyone still remember this game, simply say is set up the obstacle in the middle of the cat surrounded on the win (Orange Circle is the obstacle), and the cat can be in the surrounding six directions in the non-barrier area escape, If the cat escapes to the circle on the four-week border, the player fails and the interface is Jiangzi:



And then in the Qt bar inside also shared a bit: http://tieba.baidu.com/p/3241650033

Because at that time to do this when the main is to practice the QML, so hastily made a random direction selection algorithm (should not be qualified to call algorithm it ...) And then the kitten is dumb enough to go back to the border ...


Recently suddenly turned to this thing, still want to make this hasty things complete a little, and then spent a few days, first increased the two game difficulty, one is the original and most of the network version of the use of greed-based implementation, said straightforward point is to hit the table, find the shortest access, and then run in that direction. But this method is easy to be used by the player "digging traps" way to break down, most of the "strategy" is based on this. The second one is the subject of this blog post-- the game is almost impossible for humans to win . But we'll talk later, here we go. O (∩_∩) o~


After that, in order to play the game on the phone, adjust the interface layout to adapt to different screen sizes, and then have the following apk:

HTTP://PAN.BAIDU.COM/S/1EQ0EPHG, the last picture above, the more difficult to select the content.



For Mao apk to put Baidu network disk, originally also want to try this little game can be in Baidu 91 assistant and Pea pod on line, after all, conscience application, without any permission and no free advertising. But in the end, because the issue of copyright did not succeed, think about it, after all, except that the code is written by himself, the others are used by others of the things-.-

Then this little game is hosted on GitHub, and if you are interested, you can also maintain it together:

Https://github.com/uCloudCastle/ShenJingMao


OK, then the chatter continues to become a rakugo, quickly began our positive part:

We can take another look at the above three difficulty options, "Happy Mode" is the random direction, "normal mode" is greedy method + maximum path algorithm, this algorithm is more useful in most cases, but if the following:



The number on each border represents the number of steps required from that point to the boundary, and we set the barrier value directly to 100, because there are 81 points in total, of course, this 100 is represented by a const global amount, and the dimension of the map, as much as possible to ensure the universality of the code.

In addition to the boundary value and the barrier value, the number on the other points is the minimum value of 6 points around the number of +1, with such a matrix, the nerve cat in the mobile only need to move to the smallest side of the lattice on it.

So, "digging traps" to catch the nerve cat's method is: Now the player sealed "1" for the green lattice, the nerve cat will only to the left of the "1" move, and then the player sealed "0", the nerve cat back to "2", the player again seal "3", on the Gameover.


The question now is, if the cat is able to know if the player is in the "Make trap" in the green "1" grid, or does it look like the lure of the road should not go?



For example, if the current turn of the nerve cat move, and the cat in the "2" position, "1" of the periphery only that a value of "0" of the boundary point, the nerve cat can move to "1" place? The answer depends on the value of the other squares around the "1", but a slightly sensible player should not let the cat out of the "0" , so the cat eventually wins and must embark on a node with two "0" value boundaries:



At this time, the nerve cat in the "E1" place, the player no matter which "0" can not stop the nerve cat escaped, we put such a "1" node named "E1". Also say that if the nerve cat arrives "E1", it has already won. Then again, if the nerve cat arrives at "E2", The end is written (and of course more than two "E1"):



Notice here that we're going to start with the "lattice around one border" problem again:



This "1" around only one exit, but when the nerve cat arrives at that point, the player is bound to be around this "0" seal, and then the nerve cat will be able to reach the "E1", that is to say, the "1" is equivalent to the smallest "Ex", so on its "E2" also set up.

There is a more general corollary to this situation: if the boundary "0" is considered "E0", then the parent node of the two "Ex" (x for any number ) is the larger of the two, that is to say, the following figure is established:



Here the problem seems to be clear, we continue to push upward to "E3", "E4". As long as the cat is on any of these points, the game is over, but in this map, most of the structure is the following:



There is a boundary is shared by two "E1", if Cat is currently located in "E2", the player is a little bit smarter not to seal one of the "E1", but the red arrow points to the key boundary point key, then regardless of the nerve cat reached which "E1", the player can intercept it. (for example, the nerve cat goes to the blue arrow, and the player then seals the green Arrow points.) )

Therefore, we need to ensure that the topology is a true tree structure, and that the child nodes of each node cannot be shared by other members. In 9*9 and the map containing the barrier nodes, if we want to start from the boundary point to establish completely independent of the "E1", "E2", "E3". The tree-shaped network, on the one hand, is particularly computationally large, because each boundary "0" point to a new "E1" name will affect each layer from bottom to top of the calculation, on the other hand, the map structure determines to be able to as "E3" point is so few, "E4" don't think about it, Shows how many non-barrier nodes are required to form a "E3" node.



As a result, it is better to expand outward from the node where the nerve cat is located, even if the nerve cat is not currently on the "Winning path", it is not easy for the player to identify each path to "E3", "E2" and "E1" and destroy it, and we use a multi-tree structure to store the path of the neuro-cat In order to ensure that the nodes do not jump to their ancestors to create an infinite loop, this is done by the hierarchical traversal method, and Qhash to store the ancestors and their number of layers. The child nodes of each node may be the same as the child nodes of their sibling nodes, so as to ensure that the nerve cat in a relatively close to the center or more obstacles can still find a non-"win" but still better path, the player slightly wrong step, the path may become a "win" path.

The data structure used is the following:

struct TreeNode {    int val;    int depth;    Qvector<treenode*> childlist;    TreeNode (int v = nullinit, int d = nullinit):    Val (v), depth (d) {}    ~treenode ()    {        treenode* n;        foreach (n, childlist)            delete n;    }; struct Pathstruct {    qhash<int, int> M_hash;    TreeNode *m_node;    Pathstruct () {m_node = NULL;}    ~pathstruct () {delete m_node;}};


call, more details can be posted on the above Github to see, as long as the idea to the code is the second, and finally to leave a map for everyone, guess where the nerve cat will run? ~


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Probably the hardest cat to surround--an algorithm for finding a winning path

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.