Transfer from http://blog.csdn.net/flqbestboy/article/details/8222603
(I don't know where he turned from)
Game problem
If you want to study game theory carefully, I highly recommend that Professor Thomas S. Ferguson of the University of California, who has written and offered this textbook for free, has benefited me too much. (If your English level is not enough to read it, I can only say, I'm afraid you're not in need of a "game theory".) )
Nim game is one of the most classical models in game theory. ), it has very simple rules and incredibly beautiful conclusions, it is probably the most appropriate to start to understand game theory.
The Nim game is a combination game (combinatorial games), which, to be exact, belongs to "impartial combinatorial" (hereinafter referred to as ICG). The game that meets the following conditions is ICG (probably less rigorous): 1, two players, 2, two players alternately move the game (move), one step at a time, the player can (generally) a limited number of legal moves in the set of any one of the mobile; 3, for any possible situation of the game, The legitimate move collection depends only on the situation itself, and does not depend on the player's operation, any previous operations, the number of dice, or any other factor; 4. If a player moves and the legal movement set of the situation is empty (that is, it cannot be moved at this time), the player is negative. According to this definition, many of the daily games are not ICG. For example, chess does not meet the condition 3, because the red side can only move the red son, Black can only move sunspots, the legal movement of the set depends on the player's turn to operate.
The usual NIM game is defined as: There are a number of stones, the number of stones per heap is limited, the legal movement is "choose a heap of stones and take a few (not to take)", if the turn of a person when all the gravel heap has been emptied, then the negative (because he does not have any legal movement).
The game looks a little complicated, let's start with a simple case study. If it is your turn, leaving only a heap of stones, then the winning strategy at this time must be to take this heap of stones all take one and not to the opponent left, and then the opponent lost. If there are two piles of unequal stones, the winning strategy is to make the two piles equal by taking a pile of pebbles, and if the opponent takes a number of them in a heap, you can take the same number of stars in the other pile until you win. If you are faced with two piles of equal stones, then you do not have any strategy to win, but the opponent can follow the above strategy to ensure victory. If it's three piles of stone ... It seems difficult to analyze, it seems we have to rely on some other useful (preferably stylized) analysis methods, or we would better be able to design a winning strategy can find a winning strategy algorithm.
Define P-position and N-position, where P stands for previous,n on behalf of next. Intuitively, the last move of the people have a winning strategy is the situation is p-position, that is, "the next can be guaranteed to win" or "the initiator will be defeated", now turn to move the person has a winning strategy is the situation is n-position, that is, "the initiator can be guaranteed to win." The more rigorous definition is:
1. A situation where no movement is possible (i.e. terminal position) is p-position;2. The situation that can be moved to P-position is n-position;3. All movements result in n-position The situation is p-position。
By this definition, if the situation is impossible to reproduce, or if the positions collection can be topological sorted, then each position is either p-position or n-position, and can be computed by definition.
Take the Nim game as an example to do some calculations. For example, I just said that when there are only two piles of stone and two piles of stone equal when there is a winning strategy, that is, this is a p-position, the following we rely on the definition of proof (3,3) is a p-position. First (3,3) sub-situation (that is, through the legal movement can be caused by the situation) (0,3) (1,3) (2,3) (obviously the location of the exchange of stone heap does not affect its nature, so the (x, y) and (y,x) as the same situation), only need to calculate the nature of these three kinds of situation can be. (0,3) The sub-situation has (0,0), (0,1), (0,2), where (0,0) is obviously p-position, so (0,3) is n-position (as long as you find a sub-situation is p-position can be described as n-position). The successor (1,3) is p-position (because the only sub-situation (0,1) is n-position), so (1,3) is also n-position. It can also be proved that (2,3) is n-position. So all the 3,3 are n-position, it's p-position. By a simple mathematical induction, it is very hard to prove that "there are two piles of gravel when the situation is p-position and only when the number of these two stones is equal".
According to the above process, a recursive algorithm can be obtained-for the current situation, recursive calculation of its properties of all sub-conditions, if there is a certain sub-situation is p-position, then the move to this sub-situation is the winning strategy. Of course, you may have been acutely aware of the large number of overlapping sub-problems, so you can use DP or memory search methods to improve efficiency. But the problem is, with this algorithm, for the situation of a Nim game (A1,a2,..., an), in order to determine its nature and find a winning strategy, it is necessary to calculate the nature of O (A1*a2*...*an) situation, regardless of memory can not reduce the complexity of this time. So we need a more efficient way of judging the nature of the Nim game situation.
Just say the conclusion well. (Bouton's theorem) for a Nim game situation (A1,a2,..., an), it is p-position when and only if a1^a2^...^an=0, where ^ represents XOR (XOR) operation. What, isn't it amazing? When I saw it, I thought it was magical, totally unreasonable, and it was related to the different or the operation. But the proof of this theorem is not complicated, basically it is according to two kinds of position proof.
According to the definition, it is proved that a method of judging the nature of position is correct, only three propositions are proved: 1, this judgment will all terminal position sentenced to p-position;2, According to this judgment is sentenced to n-position situation can be moved to a p-position;3, according to this judgment was sentenced to p-position situation can not move to a p-position.
The first proposition obviously, terminal position only one, is all 0, xor or still is 0.
The second proposition, for a situation (A1,A2,..., an), if the a1^a2^...^an!=0, there must be a certain legal movement, the AI will be changed into AI ' after satisfying 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.
The third proposition, for a situation (A1,A2,..., an), if the a1^a2^...^an=0, there must be no legal movement, the AI will be changed into AI ' after satisfying A1^a2^...^ai ' ^...^an=0. Because the XOR operation satisfies the extinction rate, the A1^a2^...^an=a1^a2^...^ai ' ^...^an can get Ai=ai '. So changing AI to Ai ' is not a legitimate move. The certificate is completed.
According to this theorem, we can judge the nature of a nim's situation in the time of O (n), and if it is n-position, we can find all the winning strategies in O (n) time. The NIM problem is basically the perfect solution.
In the next section, "Sprague-grundy function", we will face more variants related to the Nim game and see the A1^a2^...^an of the Nim game as a wide range of meanings. Please look forward to it.
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. (combined with a map to think)
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 up to get the original game of the SG function, you can solve the original game
On the game theory of "turning"