1th Chapter Game--nim (2) Game analysis

Source: Internet
Author: User

NIM (2) "Twist" game analysis 1. Problem

There are n blocks of stone and two players A and B, player a first divides the stone into piles and then follows Baba ... In the order of the continuous take the stone, the remaining stones can be the light of the player won. Each time a stone is taken, each player can choose from a heap of stones, whichever number (greater than 1) of the stone is in the heap.

Excuse me: Does player A have a winning strategy? How do you assign and take stones to make sure you win?

2. Solution and Analysis

It is said that the game originated in China, the English name "NIM", is from the Cantonese "pick" (the meaning of the object) transliteration, through the year to work in the Americas, the Chinese spread out, this game a common variant is to 12 coins divided into three columns [3,4,5] and then start playing. We are talking about the "twist" game in the general sense.

How are you going to solve this problem in the face of the interviewer's aggressive gaze?

In the interview, the interviewer's focus is not "what"--whether to remember the solution of a problem, the exact age of a historical event, the branch of a rule in the C + + language about the inheritance of classes, and so on. What the interviewer wants to know is "how" – how the candidate thinks and learns.

So, the candidate has to show his ideas. Solving such problems should begin with the most basic exceptions. We use N to denote the number of heaps of stones, and m for the total number of stones.

When N=1, there is only a heap of stones-obviously no matter how many stones you put, your opponent can take all the light at once, you can't put it that way.

When the n=2, there are two piles of stone, the simplest case is each pile of stones each have a stone (the first)-let the opponent, no matter what you can win. We put this on both sides of the rational way, you must be able to win the situation is called security situation.

When n = 2,m > 2 o'clock, since (1, 1) is a security situation, then (1, x) is not a security situation, because the opponent just after a conversion, you can turn (1, X) into (1, 1), and then you go, you lose. Since (1, X) is unsafe, what about (2, 2)? After analysis, (2,2) is safe, because it can not be a step into such a security situation. So we seem to be able to infer (3, 3), (4, 4), until (x, x) is a security situation.

We initially concluded that if the number of stones were even, they would be divided into two piles, each with the same number of piles. This way, no matter how your opponent takes it, you just have to make sure you're safe (x, x) and you can win.

Well, what if the number of stones is an odd number?

When m=3, there are two cases, (2, 1), (1, 1, 1), both of which will win first.

When m=5, it is similar to m=3. No matter how you put it, you will win first.

What if m=7? Situation more up, head some dizzy, it seems to be the first to win.

Here we get a very important stage conclusion:

When the placement method is (1, 1,..., 1), if the number of 1 is an odd number, the first to win, if the number of 1 is an even number, then the first person will lose.

When the placement method is (1, 1,..., 1, x) (more than 1, plus a greater than 1 x), the first person will win. Because:

If 1 has an odd number, the first person can take X-1 from this heap one at a time, leaving an even number of the next people who will lose.

If there is an even number of 1, plus an X, the first person can take x all at once, leaving even a number of the next man will lose.

Of course, the game is played by two people, there are other kinds of pendulum, for example, when m = 9, we can be placed as (2, 3, 4), (1, 4, 4), (1, 2, 6), and so on, so many stone, they are independent, and each other, how to analyze the winning strategy? The key is to find out whether there is a feature in this series of changes that always determines winning or losing. This time, you have to test the real Kung fu, we have to think about the first year of college mathematical Logic class to go to the XOR operation. The XOR rule is as follows:

XOR (0, 0) = 0

XOR (1, 0) = 1

XOR (1, 1) = 0

First we look at the whole game process, we start with n Heap stone (M1, M2, ..., Mn), both wits, the stone has been descending to all 0 (0, 0,..., 0).

When M is an even number, our winning strategy is to divide m into the same two parts so that we can win.

Start: (M1, M1) The result of their XOR (M1, M1) = 0

Midway: (M1, M2) opponent no matter how to take from this heap of stones, XOR (M1, M2)! = 0

US: (M2, M2) We still put two piles of disguise and so on. XOR (M2, M2) = 0

...

Last: (M2, M2) we win

Similarly, if M is an odd number, we divide the stone into (1, 1, ..., 1) odd heaps, XOR (1, 1,..., 1) [Odd]!=0. At this time, the other side can take a whole heap, XOR (1, 1,..., 1) [even several]=0, so go on, we will lose.

We generalize to M is odd, but the number of stones per heap is not limited to 1 cases, see the law of the XOR value:

Start: (M1, M2, ..., Mn) XOR (M1, M2, ... Mn) =?

Midway: (M1 ', M2 ', ... Mn ') XOR (M1 ', M2 ', ... Mn ') =?

Last: (0, 0, ..., 0) XOR (0,0, ... 0) =0

Unfortunately, it can be seen that when there are odd numbers of stones, no matter how you divide the heap, XOR (M1, M2, ...). Mn) is always not equal to 0!                                                                                            Because there is bound to be an odd number of rocks (the binary represents the lowest of 1), the minimum bit of the XOR result is definitely 1. [Conclusion 1]

Again unfortunately, it can be proved that when XOR (M1, M2, ... MN)! = 0 o'clock, we always just need to change the value of one MI, you can let Xor (M1, M2, ... Mi ', ...                        Mn) = 0. [Conclusion 2]

More unfortunately, it can be proved that when XOR (M1, M2, ... Mn) = 0 o'clock, any change in M-value (taking a stone) will allow XOR (M1, M2, ... Mi ', ...                      MN)! = 0. [Conclusion 3]

With these three "unfortunate" conclusions, we have to admit that when M is an odd number, no matter how divided, always the first person to win.

Still don't believe it? Then let's try it: when m=9, randomly divided into piles (1,2,6)

Start: (1,2,6)

1=0 0 1
2=0 1 0
6=1 1 0
Xor=1 0 1 is xor (1,2,6)!=0

B Initiator: (1, 2, 3), that is, take three from the third heap, get (three-way)

1=0 0 1
2=0 1 0
3=0 1 1
xor=0 0 0 So, XOR (=0)

Party A: (1, 2, 2) XOR (1, 2, 2)!=0.

Party B: (0, 2, 2) XOR (0, 2, 2) =0

...... A party continues to be recalcitrant ...

Party B Last: (0, 0, 0), XOR (0, 0, 0) = 0

Well, through the above analysis, we not only know the answers to such questions, but also know the rules of the game, and how to win. XOR, the operation we learned very early on, helped a lot here. We should say Orz to XOR!

Interested readers can write a program that returns when the input is (M1, M2, ..., Mn), how to take the stone, in order to have the possibility of winning. For example, when the input is (3, 4, 5), the program returns (1, 4, 5)-So it victory! The procedure is as follows:

1  Packagechapter1youxizhilenim2;2 /**3 * NIM (2) "Twist" game analysis4  * @authorDELL5  *6  */7  Public classNIM2 {8     Private int[] A;//array of Heaps9     //Construction MethodTen      PublicNIM2 (int[] a) { One          This. A =A; A     } -     //get a way to take a stone -      Public voidGetResult () { the         intTemp//temporary storage calculation Results -         intsum = a[0];//stores the XOR result of all array elements -         inti,j; -System.out.print ("Raw Heap Condition: ("); +          for(i=0;i<a.length-1;i++){ -System.out.print (a[i]+ ","); +         } ASystem.out.println (a[a.length-1]+ ")"); at          for(j=1;j<a.length;j++){ -Sum ^=A[j]; -         } -          for(i=0;i<a.length;i++){ -temp = sum^A[i]; -             if(temp<a[i]&&temp>=0){ inA[i] =temp; -                  Break; to             } +         } -         if(i>=a.length) theSystem.out.println ("How to take can not win!") "); *         Else{ $System.out.print ("After the completion of the sub-heap situation: (");Panax Notoginseng              for(i=0;i<a.length-1;i++){ -System.out.print (a[i]+ ","); the             } +System.out.println (a[a.length-1]+ ")"); A         } the     } +      Public Static voidMain (string[] args) { -         int[] A = {3,4,5}; $NIM2 Nima =NewNIM2 (a); $ Nima.getresult (); -         int[] B = {1,2,6}; -NIM2 nimb =NewNIM2 (b); the Nimb.getresult (); -     }Wuyi  the}

The program has a time complexity of O (n) and N is the number of heaps.

The results of the program run as follows:

Original sub-heap situation: (3,4,5) After the completion of the sub-heap situation: (1,4,5) The original sub-heap situation: (1,2,6) After the completion of the sub-heap situation: (a)
3. Extension issues

1. If the rule is reversed, the person who takes all the stones loses, how to control the situation?

The analysis method is the same.

2. If you can pick any K heap at any time and take any stone from it, how do you find a winning strategy?

Analysis method with the 1th chapter of Game Music--nim (1) A row of pebbles in the game expansion problem.

1th Chapter Game--nim (2) "Twist" game analysis

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.