Nim game (Collection full version), nim full version

Source: Internet
Author: User

(Reprinted) Nim game (Collection full version), nim full version

Nim game Overview:

Do you still remember this game?
Given n columns of pearls, the two take turns to take the pearls. Each time they take at least one pearl in a column, they cannot take it in the two columns. Finally, the person who received the pearl lost.
Later, I saw in a piece of information that this kind of game was called "Nim )". It is said that it originated from China and was carried out by slaves who were sold to the Americas. Hard-working workers, in their spare time, play games with stones to eliminate loneliness. Later, it was spread to senior people who used Pennies to play on the bar cabinet.
The most famous method is to put 12 penny in three columns: 3, 4, and 5, and win with the copper. Later, we found that the first person to take two in the three columns and turn them into 1, 4, and 5. Then the game became boring. As a result, we increased the number of columns and the number of copper plates, which made people feel irregular and difficult to grasp.
Until the beginning of this century, Chales Leonard Bouton, an associate professor of mathematics at Harvard University, presented an extremely detailed analysis and proof using the binary representation of numbers, answers the general rules of this game.
The general rule is to set the win-win mode for a single player.
The variant of this rule is that the person who gets the copper is lost, as long as you pay attention to a special form (only one column is not 1), you can!
Many people write this method as a computer program to confront people. People who don't know how to handle it are cheated and are amazed by the magic of the computer. In fact, it is simply because it is faster than humans, and the speed of converting data into binary data is faster than humans.
(K12 Education Forum)

Mathematical Theory of Nim games:

Nim games are the most classic model in game theory. They have simple rules and the conclusion that they are not superior to each other.
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.

NormallyDefinition of Nim gamesYes: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 several stones away (you can't help but take them )", if it is the turn of a person, all the stone heaps have been taken empty, it will be 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 an algorithm that can find a winning strategy when there is a winning strategy.

Define P-position and N-positionWhere P represents Previous, and N represents 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.

(From Baidu encyclopedia)

Nim game image details:

Nim play is a game where two people face several coins (or stones. With k> = 1 heap of coins, each heap contains N1, N2 ,...... NK coins. The goal of the game is to choose the last remaining coin. The rules of the game are as follows: 1. two gamers play the game in turn (game player I and game player II); 2. when it is the turn of every player to fetch, select a heap of these stacks and take at least one coin from the selected heap (the player can take all the coins from the heap he selected); 3. when all the heap changes to an empty heap, the winner is the final gamer. The variables in this game are heap k and heap coin N1, N2 ,...... Nk. The corresponding combination problem is: Determine whether the player I wins or the player II wins and how the two players should take the child to ensure that they can win (the winning strategy ). To further understand the Nim sub-game, we examine some special cases. If the game starts with only a pile of coins, player I wins by taking all the coins away. There are now 2 heaps of coins, and the number of coins is N1 and N2, respectively. A player's victory does not depend on the exact values of N1 and N2, but on their equality. Set N1! = N2: the number of coins that the gamer I took from the heap is equal to that of the two heap coins. As a result, the number of sub-Coins obtained by the gamer I is equal to that of player II and eventually wins. However, If N1 = N2, game player II only needs to take the same number of coins in the other heap according to the number of game player I, and the final winner will be game player II. In this way, the two heap sub-winning strategies have been found. How can we extend the two-heap sub-policy to any heap number? First, let's recall that each positive integer has a corresponding binary number, for example: 57 (10) à 111001 (2), that is: 57 (10) = 25 + 24 + 23 + 20. Therefore, we can think that each heap of coins is composed of two power sub-heaps. In this way, the heap containing 57 coins can be considered as a sub-heap consisting of 25, 24, 23, and 20 respectively. The size of each heap is N1, N2 ,...... The general Nim sub-game of Nk. Represent each number Ni as its binary number (the number of digits is equal, and 0 is added before unequal values): N1 =... A1a0N2 = bs... B1b0 ...... Nk = ms... M1m0 if the number of sub-heaps of every size is an even number, we will say that the Nim sub-game is balanced, and the sum of the corresponding bits is an even number, otherwise, it is called an unbalanced bit. Therefore, the Nim sub-game is balanced when and only when:

As + bs +... + The MS is an even number

......

A1 + b1 +... + M1 is an even number.

A0 + b0 +... + M0 is an even number.

So we can get a winning strategy: player I can win in a non-balanced sub-game, while player II can win in a balanced sub-game. We used a two-heap coin Nim sub-game as an experiment. When the game starts, the game is in an unbalanced state. In this way, the gamer I can use a sub-Fetch method so that the sub-fetch is left to the gamer II as a balanced game, and then no matter how the gamer II gets the sub-play, the player I must be a non-balanced game. After the player II obtains the sub-state in the last balanced state, game Player I can take all the coins at a time to win. If the game is in a balanced status at the beginning of the game, take the sub-account in the above method and final player II will win. The following uses this winning strategy to consider 4-heap Nim sub-game. The heap size is 7, 9, 12, and 15 coins respectively. In binary format, the numbers are 0111,1001, 1100, and 1111 respectively. The following table is displayed:
 

23 = 8

22 = 4

21 = 2

20 = 1

Heap with a size of 7 0 1 1 1
Heap with a size of 9 1 0 0 1
Heap with a size of 12 1 1 0 0
Heap with a size of 15 1 1 1 1
It can be seen from the balancing condition of the Nim sub-game that this game is a non-balanced sub-game. Therefore, the gamer I will surely win the final victory in a sub-game based on the winning strategy. There are many specific practices. Game Player I can take 11 coins from the heap of 12 to balance the game (as shown in the following table ),
 

23 = 8

22 = 4

21 = 2

20 = 1

Heap with a size of 7 0 1 1 1
Heap with a size of 9 1 0 0 1
Heap with a size of 12 0 0 0 1
Heap with a size of 15 1 1 1 1
Then, no matter how game player II gets the child, game player I still balances the game after the child is obtained. In the same way, game player I can also choose a heap of 9 and take 5 coins while 4 are left, or, game Player I removed 13 from the heap of 15 and left 2.

In the final analysis, the key to a Nim sub-game is the status (balanced or unbalanced) of the game at the beginning and whether the first player can play the game based on the winning strategy of the sub-game.

(Above

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.