Nim games, XOR or problem solving, God proof __ algorithm Design

Source: Internet
Author: User

Address: Http://baike.baidu.com/view/1101962.htm?fr=aladdin#4

Nim Game Editor This entry is missing the Information Bar , business card map , add related content to make the entry more complete, but also can quickly upgrade, hurriedly to edit it. Nim game is the most classical model in game theory (one), it has very simple rules and incomparable conclusion NIM game is a combination game (combinatorial Games), accurately speaking, belongs to the "impartial combinatorial Games" (hereinafter referred to as ICG). Directory

1 conditions

2 definition

3 Computation (DP, memory Search)

4 Conclusion

5 Proof1 Condition EditionGames that meet the following criteria are ICG (perhaps less rigorous): 1, there are two players, 2, two players alternate to the game Movement (move), each step, the player can be (in general) a limited number of legitimate moving a set of a move; 3, for any possible situation of the game, The legitimate move set depends only on the situation itself, it does not depend on the player's turn, any previous operation, the number of dice, or any other factor; 4. If a competitor moves, and the legal move 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 day-to-day games are not ICG. For example, chess does not meet the conditions of 3, because the red can only move the red son, Black can only move sunspots, the legitimate move set depends on the turn of the player to operate.2 definition editsThe usual NIM game is defined as: There are a number of pebbles, each with a finite number of stones, the legal move is to "pick a bunch of stones and take a few (without)", and if it's time for someone to have all the gravel heaps empty, it's a negative (because he doesn't have any legal moves at the moment). The game looks a bit complicated, start with a simple case. If it is your turn, only a pile of stones, then the winning strategy at this time must be to take the whole pile of stone is not left to the opponent, and then the opponent loses. If two piles of unequal stones are left, the strategy is to make the two piles equal by taking more than one pile of stones, and if the opponent takes several in a certain pile, you can hold the same number of numbers in the other pile until the victory. If you are faced with two piles of equal stones, then you do not have any strategy to win, but the opponents can follow the above strategy to ensure victory. If it is three piles of stones ... It seems difficult to analyze, it seems that we have to use some other useful (preferably stylized) analysis methods, or, we should be able to design a win strategy when we can find a winning strategy algorithm. Define P-position and N-position, where p represents previous,n on behalf of next. Intuitively speaking, the last move people have a winning strategy of the situation is p-position, that is, "the next can be guaranteed to win" or "the first time will fail", now turn to move people have a winning strategy is the situation is n-position, that is, "the tempo can guarantee victory." A more rigorous definition is: 1. The situation where no movement can be carried out (i.e. terminal position) is P-position 2. The situation that can move to P-position is n-position;3. All movement leads to the n-position situation is p-position. According to this definition, if the situation is not possible to reproduce, or the positions set can be topological sorted, then each position either p-position or n-position, and can be computed by definition.3 Computing (DP, memory search) EditTake the Nim game as an example to do a calculation. For example, I just said that when there are only two piles of stone and the number of stones is equal, there is a win strategy, that is, this is a p-position, we rely on the definition to prove (3,3) is a p-position. First (the 3,3) (i.e. the situation that can be caused by legal movement) has (0,3) (1,3) (2,3) (apparently swapping the position of the gravel heap does not affect its nature, so x,y and (y,x) are the same situation, you just have to figure out the nature of the three scenarios. (0,3) (0,0), (0,1), (0,2), in which (0,0) is obviously p-position, so (0,3) is n-position (as long as you find a child of p-position can be described as n-position). The successor (1,1) of (1,3) is p-position (because the only child (0,1) of (1,1) is n-position), so (1,3) is also n-position. The same can be proved (2,3) is n-position. So all the 3,3 are n-position, it's p-position. By a simple mathematical generalization, it can be strictly proved that "when there are two heaps of stones, the situation is p-position when and only when the number of stones is equal". According to the above process, we can get a recursive algorithm-for the current situation, recursive calculation of the nature of all its sub-situation, if there is a child situation is p-position, then the move to this child situation is a win strategy. Of course, you may be acutely aware of a large number of overlapping problems, so you can use DP or a mnemonic method to improve efficiency. The problem is, using this algorithm, for a NIM game (A1,a2,..., an), to determine its nature and to find a winning strategy, it is necessary to calculate the nature of the O (a1*a2*...*an) Situation, no matter how memory can not reduce the complexity of the time. So we need a more efficient way of judging the nature of the Nim game situation.4 Conclusion Editions(Bouton ' s theorem) for the situation of a Nim game (A1,a2,..., an), it is p-position when and only if a1^a2^...^an=0, where ^ represents an XOR operation. How, is not very magical. When I saw it, I thought it was magical, and it was completely unreasonable to relate to XOR or operation. But the proof of this theorem is not complex, basically is according to two kinds of position proof.5 Proof EditionAccording to the definition, it is proved that a method of judging the nature of position can only prove three propositions: 1, this judgment will all terminal position as P-POSITION;2, The situation that was judged to be n-position on the basis of this judgment must be moved to a certain p-position;3, according to which the p-position situation could not be moved to a p-position. The first proposition is clearly that terminal position is only one, that is, 0, different or still 0. The second proposition, for a situation (A1,A2,..., an), if a1^a2^...^an<>0, there must be some legal movement, the AI changed to Ai ' after A1^a2^...^ai ' ^...^an=0. May be set a1^a2^...^an=k, there must be an AI, its binary representation at the highest level of K is 1 (otherwise k the highest bit that 1 is how to get). At this time Ai^k<ai must be established. Then we can change the AI into 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 a1^a2^...^an=0, there must be no legal move, the AI changed to Ai ' after A1^a2^...^ai ' ^...^an=0. Because the XOR or operation satisfies the elimination rate, by A1^a2^...^an=a1^a2^...^ai ' ^...^an can get Ai=ai '. So changing AI to AI is not a legitimate move. Certificate of Completion. According to this theorem, we can judge the nature of a nim's situation in O (n) time, and if it is n-position, we can find all the winning strategies in O (n) time. The NIM problem was basically solved perfectly. Entry Tags: mathematical game theory

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.