Nim Game Strategy

Source: Internet
Author: User

Nim takes a child game which is made up of two people facing several piles of coins (or pebbles, or. The game, played by two people, with k>=1 heap coins, each heap contains n1,n2,n3,n4.....,nk coins, the purpose of the game is to select the last remaining coins. The rules of the game are as follows:

1) game people alternately play;

2) When it is time for each player to take the time, select a bunch of these coins and take at least one coin from the selected heap (you can take all the coins in the selected heap to get the remaining empty heap).

When all the heaps become empty heaps, the game is over. The last person to take the child (i.e. the one who can take all the coins in the last pile) wins.

The variable in this question is the heap number k and the number of coins in each heap n1,n2,n3, ... , NK, determines whether the player 1 or the Player 2 wins and how the player should take the son to win.

The number of coins per heap ni represents Singga to 2:

N1=as. A1a0

N2=bs....b1b0

.......

Nk=rs...r1r0

We can assume that the size of all the heaps is a base number of 12 with the same number of bits, by means of the preceding 0. We call the game balanced when and only if

As+bs+...+rs is even

.......

Ai+bi+...+ri is even

.......

A1+B1+...+R1 is even

A0+b0+...+r0 is even

If the game is not balanced, then it is said that the game is non-balanced. If the Ai+bi+...+ri is an even number, then it is said that the first bit is balanced, otherwise it is called unbalanced.

The conclusion is:

Game Man 1 is able to win in the unbalanced Nim-taking sub-game, while the player 2 can win in the balanced Nim-taking sub-game.

The NIM game is non-balanced. Make the largest non-equilibrium position J-bit. Then, the game person 1 in such a way to take the son, so that the game is left to 2 of the game is balanced to take the child game. Make J is the largest non-equilibrium bit, game man 1 Select the first J bit is a heap of 1 and then take some coins from the selected heap to make the game a balanced game. Since then, regardless of the game 2 How to take the son, the game is unbalanced, the game people 1 coins always make the game is balanced, then the game people 1 can win. If the game starts from a balanced state, the game player 1 takes some coins, making the game into a non-equilibrium, game people 2 take coins to make the game balance, so that can win.

Here's a simple example

Number of coins 2^3=8 2^2=4 2^1=2 2^0=1
11 1 0 1 1
6 0 1 1 0
10 1 0 0 0
8 1 0 0 0

As can be seen from the table, the simple look is the first column, the second column, the number of the fourth column 1 is odd, the game is non-balanced, the maximum non-equilibrium is 2^3=8, so take a this bit is 1 to take the coin, here take 11 coins of the heap, take 5, make the game into a balanced, as follows

Number of coins 2^3=8 2^2=4 2^1=2 2^0=1
4 0 1 1 0
6 0 1 1 0
10 1 0 0 0
8 1 0 0 0

In so doing, the game man will eventually win.

Nim Game Strategy

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.