Game Game Summary

Source: Internet
Author: User

1. Ba Shi Game

A heap of stones, there are N, two people take turns, take at least 1 at a time, at most to take M, take the last stone people win

Suppose a heap of stones have n=m+1 because one can only take m, regardless of the number of times to take, the left will always take the remaining, this must be the initiator negative

Then find the rule of winning:

A pair of Pebbles n= (m+1) *r+s

For the initiator should first take the S, set to take away K, the initiator and then take away m+1-k remaining stone number for (m+1) (R-1) after maintaining such a way, the first to win the winner

In short, is to leave the opponent m+1 multiples

Can be summed up as: S=0 when the

When S<>0, the initiator wins

2. Simple Stone Game

There are n heap of stones, at least one at a time, at most take the whole heap, two people take turns, each limit to take one of the piles, take the last one to win.

The 1902 winning strategy was completed by the American mathematician C.l.bouton, using the concept of binary and balanced states. The conclusion is that:
For n heap stones, the number of stones in the 1<=i<=n heap is XI, which is a state of failure when and only if X1 xor X2 xor ... Xn=0.

Look at an example:

There are 4 piles of stones, the number of which are: 7 9 12 15
Binary form is
0111
1001
1100
1111
XOR result is: 1101

1101^1001=0100=4 can take 5 out of the second pile.

1101^1100=0001=1 can also take 11 out of the third heap.

1101^1111=0010=2 or take 13 from the heap four.

For example this problem: http://www.acmicpc.sdnu.edu.cn/Problem.aspx?pid=1294

Given the n heap of stones, two people take the stone in turn, must first take a heap of stones to take another pile, and the number of other stones must be smaller than the previous one, can only take 1 or a quality number of stones at a time. If there were no stones to take, he would lose. Ask if there is a winning strategy.

In fact, for this game, we only need to judge the first heap of stones, that is, the smallest number of stones in the pile

Code:

#include <iostream>#include<algorithm>#include<math.h>using namespacestd;intIsPrime (intN) {    intS= (int) sqrt (n); if(n==1)return 1;  for(intI=2; i<=s;i++){        if(n%i==0)            return 0;  Break; }    return 1;}intMain () {intn,a[100000],x,i,j,t;  while(cin>>N) {         for(i=0; i<n;i++) Cin>>a[i];//the number of stones per heapSort (a,a+N);  for(j=1; j<=a[0];j++){                if(IsPrime (j) &&j<=a[0]) a[0]=a[0]-J; X=0;  for(t=0; t<n;t++) x^=A[t]; if(x==0) {cout<<"Yes"<<Endl;  Break; }Elsea[0]=a[0]+j;//number of stones restored            }            if(x!=0) cout<<"No"<<Endl; }        return 0;}
View Code

3. Nim Games

There are n heap of stones, the number of stones per heap is x1, x2, X3,x4......xn. Given k number A1,A2,A3,...... AK Two take turns, each person can only select a bunch,

Take out some of it, each time the amount taken must be in A1,A2,A3,...... AK, take the last one to win.

We can break the game down and think of each heap as a sub-game.

For example, there are 3 stones, the number of stones per heap, for 5,6,7, the number of stones that can be taken is {1,3,4}

It is possible to find out all the subsequent states of each heap of stones, as if n pieces move on a map, and a party B will be a sub-game and move the pieces above.

It looks very, very complex, so we need to draw on the SG function

First, define a MEX operation that represents the smallest non-negative integer that does not belong to this set. such as Mex{0,1,2,4}=3, Mex{2,3,5}=0, mex{}=0.

For a given, forward-free graph, the SG function for each vertex is defined as follows:

g (x) =mex{g (y) |y is all subsequent states of x; there are no vertices with an out edge, and the SG value is 0 because its successor set is empty.

For n pieces, the SG values corresponding to the vertices are: (a1,a2,......, an) to set the situation (A1,a2,......, an) a winning strategy for the Nim game is to turn AI into K, then a winning strategy for the game is

Move the first piece to the vertex of the SG value K;

#include <iostream>using namespacestd;/*Number of input heaps N, number of stones per heap a[] Enter the number of limited selections K, is s[]*/intMaxintA[],intN) {    intm=0;  for(intI=0; i<n;i++){        if(m>A[i]) m=A[i]; }    returnm;}intMain () {intn,a[ -],i,j,s[ -],k,*vis,*Grund;  while(cin>>N) {         for(i=0; i<n;i++) Cin>>A[i]; intSize=Max (a,n); Vis=New int[size+1]; Grund=New int[size+1]; grund[0]=0; CIN>>K;  for(i=0; i<k;i++) Cin>>S[i]; //         for(i=1; i<=size;i++) {//I is the number of stones, a[j] is the number of stones to be taken each time i-a[j] The remaining stonesmemset (Vis,0,sizeof(VIS));  for(j=0; j<k;j++){            if(s[j]<=i) {Vis[grund[i-s[j]]]=1; }             for(intk=1;; k++){                if(vis[k]==0) Grund[i]=K;  Break; }                        }        }        intx=0;  for(i=0; i<n;i++) x^=Grund[a[i]]; if(x==0) cout<<"Initiator wins"<<Endl; Elsecout<<"The win"<<Endl; }    return 0;}
View Code


Game Game Summary

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.