The SG function of game (explain why it is different or can produce results)

Source: Internet
Author: User

Given a piece with a direction-free graph and a starting vertex, the two players alternately move the piece along a forward edge and cannot be judged negative by the mover. In fact, this game can be thought of as an abstract model of all impartial combinatorial games. In other words, any ICG can be abstracted into this "graph game" by taking each situation as a vertex and a forward edge to each situation and its sub-situation. Below we define the Sprague-grundy function on the vertex with the direction-free graph. The MEX (minimal excludant) operation is defined first, which is an operation applied to a set that represents the smallest non-negative integer that does not belong to this set. such as Mex{0,1,2,4}=3, Mex{2,3,5}=0, mex{}=0. For a given, forward-free graph, define the Sprague-grundy function g for each vertex of the graph as follows: g (x) =mex{g (y) | Y is the successor of X. Consider the nature of the SG function. First, all the vertices of the terminal position, that is, the vertex without the edge, whose SG value is 0, because its successor is an empty set. Then for a g (x) =0 vertex x, all its precursor y will satisfy the G (Y)!=0. For a vertex of a G (x)!=0, there must be a successor Y that satisfies g (y) = 0. The above three sentences indicate that vertex x represents the postion that is p-position when and only if G (x) =0 (the three sentences that correspond to the definition of p-positioin/n-position are exactly the same). We can find a winning strategy for each situation by calculating the SG value for each vertex that has a non-circular graph. For example: Take the stone problem, there are 1 heap n stone, at a time can only take {1,3,4} a stone, first take the stone winner, then the number of the SG value is how much.

sg[0]=0,f[]={1,3,4},

X=1, you can take away 1-f{1} stones, the remaining {0}, mex{sg[0]}={0}, so sg[1]=1; When expressed as 1, the opponent can only select the SG value of 0, it must win;

x=2, you can take away 2-f{1} a stone, the remaining {1}, mex{sg[1]}={1}, so sg[2]=0;//said only to allow the opponent SG 1, and then let the opponent under the words, because the opponent SG value of 1, so you can let the initiator to the SG value of 0, will lose;

X=3, you can take away 3-f{1,3} stones, the remaining {2,0}, mex{sg[2],sg[0]}={0,0}, so sg[3]=1; Can make the opponent SG 0;

X=4, you can take away 4-f{1,3,4} stones, the remaining {3,1,0}, mex{sg[3],sg[1],sg[0]}={1,1,0}, so sg[4]=2;

X=5, you can take away 5-f{1,3,4} stones, the remaining {4,2,1}, mex{sg[4],sg[2],sg[1]}={2,0,1}, so sg[5]=3;

F[] need to order from small to large, this template f is starting from 1. Hash array size is the same size as f[], F

1. The number of steps can be 1~m continuous integer, directly modulo, SG (x) = x (m+1);

2. The optional step is any step, SG (x) = x;

3. The optional number of steps is a series of discontinuous numbers, calculated with GETSG ()

We can according to Sg[n] whether 0 to determine whether to win, we call the SG value of 0 is required to lose the state;


But the use of the SG function is far less simple, if the game will be a bit more complicated, for example, there is not only one chess piece, but there are n pieces, each time you can choose one to move, then, how to find the winning strategy. Let's consider the meaning of the SG value of the vertex. When g (x) =k, it is shown that for any one 0<=i<k, there is a successor Y of X that satisfies g (Y) =i. In other words, when the SG value of a piece is k, we can turn it into 0, become a 1 、......、 into k-1, but never keep k unchanged. Do not know if you can according to this association to the Nim game, the rules of NIM game is: each time the choice of a pile of k stone, you can turn it into 0, into 1 、......、 into k-1, but absolutely can not keep K unchanged.This indicates that if the SG value of the vertex at which the n piece is located is treated as n heap, the corresponding number of stones, then the NIM game every winning strategy corresponds to the original of the winning strategy of the N pieces. For n pieces, set their corresponding vertex of the SG value respectively (A1,A2,..., an), and then set the situation (A1,a2,..., an) when the Nim game a winning strategy is to turn AI into K, then the original game is a winning strategy is to move the first piece to a SG value of K vertex. For a situation (A1,A2,..., an), if a1^a2^...^an<>0, there must be some legal movement, the AI will be changed into AI ' A1^a2^...^ai ' ^...^an=0. As a a1^a2^...^an=k, there must be an AI, whose binary representation is 1 at the top of the K (otherwise the highest bit of k is how the 1 is obtained). Then Ai^k<ai must be set up. Then we can change AI to Ai ' =ai^k, at this time A1^a2^...^ai ' ^...^an=a1^a2^...^an^k=0. In fact, we still just have to prove that the situation of the multi-piece of the graph game is p-position when and only if all the pieces are in the position of the SG function XOR is 0, just now, I want to make the problem look easier, think that the n pieces are moving on a graph. But if it is not on a map, but each piece on a graph, each time you can choose a piece (that is, any one of the graph) to move, so that the conclusion does not bring any changes. So we can define the graph game and (sum of graph games): Set G1, G2 、......、 gn is n a graph game, the definition game g is G1, G2 、......、 gn and (sum), game G's Move rule is: Select a sub-game GI and move the above pieces. Sprague-grundy theorem is: g (g) =g (G1) ^g (G2) ^...^g (Gn). That is, the game's and SG function values are the XOR of all its sub-games ' SG function values. Why the SG XOR result is 0 must lose. The following is a method of judging oneself experience;We first assume that there are only two piles, the SG value is 5, 9, before we say the SG value can be simplified as a corresponding number of stones, we use binary to represent; "5" 2:0 1 0 1 "9" 2:1 0 0 1 5^9=1 1 0 0; If the initiator turns 1 0 0 1 to 0 1 0 1, which is to take 4 stones, the number of stones becomes the same; then the opponent operates and then the initiator follows the Operation then certainly is the initiator to play the last, this illustration, as long as the XOR is not 0, the initiator as long as the different or the result into 0 then follow the opponent, certainly bing, of course, if the XOR is originally 0, the opponent followed the initiator, then the initiator will lose;Then we consider the N-Heap xor, if the XOR or result is 0, indicating that the number of all heaps in the binary state of each bit of 1 equal, the initiator operation, certainly change one of the 1 changes in the heap so that the number is less, the opponent simply change the other heap and its corresponding 1 bit, so that it will be different or 0, the initiator The XOR result is not 0, the initiator only need to change the XOR result to 0, as to why it must make the XOR result become 0, has been proved before; Consider the sentence at the beginning of this article: any ICG can be abstracted into a graph game. So the concept of "SG function" and "game and" is not confined to the graph game. We define a SG value for each position for each ICG, and we can define the and of N ICG. So when we face a game composed of N games, just for each game to find out each situation of the SG value method, you can see these SG values all as Nim's gravel heap, and then according to find the way to win the NIM strategy to find the game's winning strategy. Return to the question at the beginning of this article. There are n heap of stones, every time from the 1th heap of stones to take 1, 2 or 3, you can take an odd number from the 2nd heap of stones, can be from the 3rd heap and after the stone to take any of the ... We can think of it as 3 sub-games, the 1th sub-game only a heap of stones, each time can take 1, 2, 3, it is easy to see the X-stone situation of the SG value is x%4. The 2nd sub-game is also only a bunch of stones, each can take odd, after a simple drawing can know this game has x stone when the SG value is x%2. The 3rd game has n-2 Heap stone, is a NIM game. For each situation of the original game, the three sub-game of the SG value of different or a whole game of the SG value, and then can be based on the SG value to determine whether there is a winning strategy and make a decision. In fact, as 3 sub-game or conservative, simply as N sub-game, of which the 1th, 2 sub-games as described above, 3rd and later sub-game are "1 heap of stones, each take a few can", called "Take the Stone Game", this super simple game has x stone of the SG value is obviously x. In fact, n heap of Pebbles of the Nim game itself is not n "Take the Stone game" and it. So, for us, the SG function and the "game's and" concept is not to let us assemble and create wacky games, but to try to split the seemingly complex game into several sub-games, to find out the SG function for each game that is much simpler than the original. Then all the different or get up on the original game of the SG function, you can solve the original game.

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.