Next introduce the Nim game (also quoted on Hangzhou Electric, lazy typing)
1. There are two players;
2. There are three piles of playing cards (for example: can be 5,7,9 Zhang);
3. The game both sides operate alternately;
4. Each player's operation is to select one of the stacks, and then take away any of the cards from it;
5. The party that last took the card is the winner;
Think about it for a while:
Do you remember the P-point and N-points just said? P: Must defeat, N: Win point
Let's start by giving a conclusion, here to use the arithmetic in place, XOR: ^
The game's location (X1,X2,X3) x1,x2,x3 represents the number of 3 heaps. When and only when x1^x2^x3=0, this point is a must defeat p;
Conclusion can be generalized to the general situation, that is, there are N heap, (x1,x2,x3,... xn) when and only when x1^x2^x3...^xn=0, this point is a must defeat p;
To see the proof process, link this http://acm.hdu.edu.cn/forum/read.php?fid=9&tid=10617
Exercise: HDU 2188 2149 (if you can't do it, look at the following, then think more)
The following is a description of the SG function (the kingly answer game problem)
SG is graph game, the game is abstracted into a non-circular graph
(1) A direction-free graph
(2) Player 1 moves first, starting point is x0
(3) Two players take turns to move
(4) for vertex X, the set of vertices that the player can move to is recorded as f (x).
(5) Players who cannot move will lose the game
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.
Definition: The Sprague-grundy function (x,f) of a graph is a nonnegative function g (x) defined on X, and satisfies:
G (x) = Mex{g (y): Y∈f (x)}
See here first a good understanding of the SG value is how to ask;
What is the SG value for each number if you can only take {one-to-one} at a time in the child-taking game?
X 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14. . .
g (x) 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2. . .
Look at this and the pattern above:
P-point: Even if g (x) = 0 x Point!
N-point: Even if G (x) > 0 x Point!
Practice HDU 1847 1849 1850 (If you can't do it, look at the following, then think more)
Finally, the combination of game, is to combine simple games, such as 3 heap can be seen as 3 a bunch of games.
Theorem:
Suppose the game GI's SG function is GI, I=1,..., N, then
G = G1 + ... + Gn's SG function is
G (x1,..., xn) = G1 (x1) ⊙ ... ⊕gn (xn).
The one that fits is the different or ^
See if it's the same as the NIM game conclusion?
If you want to understand the principle link here: http://www.cnitblog.com/weiweibbs/articles/42735.html
After reading the above, finish the following exercises. To understand the basic almost can be counted as a primer:
HDU 1848 1517 1536 (do not think, think, see a few more times)
In the previous installment, we looked at the Nim game and learned how to find a winning strategy. But if the NIM rules change slightly, can you find a winning strategy quickly? For example: There are n heap of stones, every time from the 1th heap of stones to take 1, 2 or 3, you can take the odd number from the 2nd heap of stones, can be from the 3rd heap and after the stone to take any of the ... At this point it seems a lot more complicated, but I believe that if you master the content of this section, a similar ever-changing problem is not a problem.
Now let's look at a game that seems more general: 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 moving side. 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-garundy 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-garundy 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 subsequent y of it satisfies 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. But the use of the SG function is far less simple. If the game becomes a bit more complicated, for example, there is not only one chess piece on the map, 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 shows that if the n pieces of the vertex of the SG value as the corresponding number of stones, then the NIM game every winning strategy corresponds to the original n pieces of the winning strategy!
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. It sounds a bit too magical-how to go around and back to the Nim game.
In fact, we just have to prove that this multi-piece of the graph game situation is p-position when and only if all pieces are located in the position of the SG function XOR is 0. This proof is almost identical to the Bouton ' s theorem of the previous section, and only requires proper modification of several nouns.
Just now, I want to make the problem seem 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.
Consider again 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 Nim's winning 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 a stone game" and?
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. This "divide and conquer" thinking in the next section of the "coin-flipping game" will be used to the fullest. Please look forward to it.
The above content is reproduced from a Daniel.
(Game SG Introduction 2)