From today on, I will use a series Article This section describes the basic knowledge of game theory, which is mainly used in Oi. Of course, I have a superficial understanding of game theory, and the style of my writing has always been a "Personal Experience" rather than a "evangelism" type. So if you want to study game theory carefully, I strongly recommend that Professor Thomas S. Ferguson from the University of California carefully write and provide this teaching material for free, which has benefited me too much. (If your English skills are not enough to read it, I can only say, I'm afraid you haven't been able to read "Game Theory" yet .)
Nim games are one of the most classic models in game theory ?), It also has simple rules and the conclusion that the game is the most suitable.
Nim games are a kind of combinatorial games. Specifically, they belong to impartial combinatorial games (ICG ). An ICG game meets the following conditions (which may not be rigorous): 1. Two players are involved; 2. Two players move the game alternately, players can choose one of the limited legal mobile sets to move; 3. Any possible situation of the game, A valid movement set only depends on the situation. It does not depend on the operator of the player in turn, any previous operation, number of dice, or other factors. 4. If it is the move of a player, the valid mobile set in this situation is empty (that is to say, it cannot be moved at this time. According to this definition, many daily games are not ICG. For example, Chinese chess does not meet Condition 3, because the Red party can only move the red child, and the black party can only move the black child. The valid moving set depends on the player's turn.
The general definition of Nim games is as follows: There are several piles of stones, and the number of each pile of stones is limited, the legal move is to "select a pile of stones and take away a number of stones (you can't help but take them)". If it is your turn, all the stones are empty, it is negative (because he does not have any legal movement at the moment ).
This game looks a little complicated. Let's start with the simple situation. If it is your turn, there will be only one pile of stones left, then the winning strategy at this time will definitely be to finish all the stones without leaving them to the opponent, and then the opponent will lose. If there are two piles of stones that are not equal to each other, the winning strategy is to take a pile of stones to make them equal. If the opponent takes several stones in a pile, you can get the same number in the other heap until the victory. If you are dealing with two equal stones, you do not have any winning strategy at this time. Instead, your opponent can follow the above strategy to ensure that you will win. If it's three piles of stones ...... It seems very difficult to analyze. It seems that we have to use some other useful (preferably stylized) analysis methods, or, we 'd better design a winning strategy that we can find when there is a winning strategy.Algorithm.
Define p-position and N-position. P indicates previous, and N indicates next. Intuitively, when the person who moved the previous move had a winning strategy, it was p-position, that is, "the latter hand can be sure to win" or "the first hand is defeated ", now it is the turn of the move to win the strategy is n-position, that is, "first hand can ensure that the victory ". More rigorous definition: 1. in a situation where no movement can be performed (that is, terminal position) is P-position; 2. the position that can be moved to P-position is n-position; 3. p-position is the case where all moves lead to n-position.
According to this definition, if the situation cannot be reproduced, or the positions set can be sorted by topology, then each position, p-position, or N-position can be calculated by definition.
Take the NIM game as an example. For example, I just said that when there are only two piles of stones and the number of the two piles is equal, the latter hand has a winning strategy, that is, this is a P-position. The following is a proof of concept (3, 3) is a p, is a P, is a P-position. First, the sub-situation (that is, the situation that can be caused by legal Movement) (apparently, the position of the stone heap does not affect its nature, so we can think of (x, y) and (Y, x) as the same situation. We only need to calculate the nature of these three situations. () Sub-situations include (), where () is obviously p-position, so) is n-position (as long as you find a sub-situation that is P-position, it can be indicated that it is n-position ). () Is followed by P-position (because () is the only sub-situation () is n-position), SO () is n-position. It can also be proved that (2, 3) is n-position. So all sub-situations in (3, 3) are n-position, which is P-position. Through a simple mathematical induction, We can strictly prove that "when there are two piles of stones, the situation is P-position when and only when the number of these two piles of stones is equal ".
Based on the above process, we can get a recursive algorithm-for the current situation, recursively calculate the nature of all its sub-situations. If there is a sub-situation that is P-position, moving to this sub-situation is a winning strategy. Of course, you may be keenly aware that there are a large number of overlapping sub-problems, so you can use the DP or memorizing search method to improve efficiency. But the problem is that using this algorithm, for a nim game situation (A1, A2 ,..., an) for judging its nature and finding a winning strategy, we need to calculate O (A1 * A2 *... * An) the nature of a situation, regardless of the memory, cannot reduce the time complexity. Therefore, we need a more efficient way to determine the nature of the NIM game situation.
Let's just draw a conclusion. (Bouton's theorem) for a nim game (A1, A2 ,..., an), it is P-position when and only when A1 ^ A2 ^... ^ An = 0, where ^ indicates an exclusive or (XOR) operation. How are you doing? When I saw it, I also thought it was amazing that there was no reason to relate the exclusive or operation. However, the proof of this theorem is not complex, basically based on the proof of two positions.
According to the definition, to prove the correctness of a method to judge the nature of position, we only need to prove three propositions: 1. This judgment judges all terminal positions as p-position; 2. When the condition is determined as N-position, it can certainly be moved to a certain position. 3. The condition that is determined as p-position cannot be moved to a certain position.
The first proposition clearly has only one terminal position, that is, all 0, exclusive or still 0.
The second proposition, for a situation (A1, A2,..., an), if A1 ^ A2 ^... ^! = 0, there must be a legitimate movement. After AI is changed to 'ai', A1 ^ A2 ^... ^ ai' ^... ^ An = 0. Set A1 ^ A2 ^... ^ An = K, then there must be an AI. Its binary value indicates that the highest bit of K is 1 (otherwise, how does one obtain the highest bit of K ). At this time, AI ^ k <AI must be established. 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 certain situation (A1, A2 ,..., an), if A1 ^ A2 ^... ^ An = 0, there must be no valid movement. After AI is changed to AI, A1 ^ A2 ^... ^ AI '^... ^ An = 0. Because the exclusive or operation satisfies the elimination rate, it is determined by a1 ^ A2 ^... ^ An = A1 ^ A2 ^... ^ AI '^... ^ An can get Ai = AI '. Changing AI to AI is not a legal move. Pass.
According to this theorem, we can judge the nature of a nim situation in O (n) time. If it is n-position, it can also be in O (N) find all winning strategies in time. The Nim problem is basically perfectly solved.
In the next section of the "SD-Grundy function", we will face more variants related to Nim games, and we will also see the A1 ^ A2 ^ of Nim games... ^ An value is more extensive. Coming soon.
Link: http://tianyi.yo2.cn/go/202984.html