Combination games and element combination games

Source: Internet
Author: User

Combination games and element combination games

Before introducing SG functions and SG theorem, let's first introduce the verbs and verbs.

The concept of "yes" and "yes": "Yes". In other words, if both parties operate correctly, they will be defeated. N: Yes. In this case, if both parties operate correctly, yes. Vertices and vertices: 1. All endpoints are vertices P. (We use this as the basic premise for reasoning, in other words, we assume this) 2. There is at least one way to go to the point P. 3. No matter how you operate, point P can only enter point N. We have simplified the objective time of winning and losing points, which is helpful for our analysis. Generally, we analyze the vertices and vertices in reverse order. We take hdu 1847 Good Luck in CET-4 Everybody! For example, when n = 0, it is obviously a point of defeat, because at this time you can no longer operate when n = 1, because you can finish all the cards at once, therefore, this is a winning point. If n = 2, it can also be completed once. Therefore, when n = 3, either one or two are left, no matter how you obtain the other party, you will face a winning point, so this point is a defeat point. And so on, you can finally get it. n: 0 1 2 3 4 5 6... position: p n p... if you find nothing, right, they just become regular and use P/N for analysis. Do you think the problem has become simple. Now you can get a little more complicated: hdu 2147 kiki's game

Now let's introduce the main character of today. The sum of combined games is usually very complex, but there is a new tool that makes the combination Problem Simple-SG functions and SG theorem.

SD-Grundy theorem (SG theorem ):

The game and SG functions are equal to the Nim and of each game SG function.In this way, each sub-game can be divided and managed, thus simplifying the problem. The Bouton theorem is the direct application of the SD-Grundy Theorem in Nim games, because the single-heap Nim game SG function satisfies SG (x) = x. If you do not know about the Nim game, please go here:

SG function:

First define the mex (minimal excludant) operation, which is applied to a set operation, indicating the smallest non-negative integer that does not belong to this set. For example, mex {0, 1, 2, 4} = 3, mex {2, 3, 5} = 0, and mex {} = 0.

For any State x, SG (x) = mex (S) is defined, where S is the set of SG function values in the subsequent State of x. If x has three successor states: SG (a), SG (B), SG (c), then SG (x) = mex {SG (), SG (B), SG (c )}. In this way, the final state of the Set S must be an empty set. Therefore, the final state of the SG function is SG (x) = 0, and only when x is the point P.

[Instance] Stone fetch Problem

There are 1 pile of n stones. Each time only {1, 3, 4} stones can be taken. What is the SG value of each number after the stone wins?

SG [0] = 0, f [] = {1, 3, 4 },

When x = 1, 1-f {1} stones can be taken away, with {0} remaining stones, so SG [1] = mex {SG [0]} = mex {0} = 1;

When x = 2, 2-f {1} stones can be taken away, with {1} remaining stones, so SG [2] = mex {SG [1]} = mex {1} = 0;

When x = 3, 3-f {1, 3} stones can be taken away, with {2, 0} remaining stones. Therefore, SG [3] = mex {SG [2], SG [0]} = mex {0, 0} = 1;

When x = 4, 4-f {1, 3, 4} stones can be taken away, and {3, 1, 0} stones are left. Therefore, SG [4] = mex {SG [3], SG [1], SG [0]} = mex {1, 1, 0} = 2;

When x = 5, you can take 5-f {1, 3, 4} stones and {4, 2, 1} stones. Therefore, SG [5] = mex {SG [4], SG [2], SG [1]} = mex {2, 0, 1} = 3;

And so on .....

X 0 1 2 3 4 5 6 7 8 ....

SG [x] 0 1 0 1 2 3 2 0 1 ....

From the above example, we can obtain the SG function value solving step, then calculate 1 ~ The SG function value of n is as follows:

1. Use array f to record the method that can change the current state.

2. Then we use another array to mark the successor status of the current State x.

3. Finally, simulate the mex operation, that is, we search for the minimum value of the unlabeled value in the tag value and assign it to SG (x ).

4. We repeat steps 2-3 to complete the calculation 1-3 ~ The value of n.

The code is implemented as follows:

// F [N]: The method that can change the current state. N is the type of the method. f [N] Must be preprocessed before getSG. // SG []: 0 ~ SG function value of n // S []: the set of x successor States int f [N], SG [MAXN], S [MAXN]; void getSG (int n) {int I, j; memset (SG, 0, sizeof (SG); // Since SG [0] is always equal to 0, I start from 1 for (I = 1; I <= n; I ++) {// reset memset (S, 0, sizeof (S) of the successor set in the previous state every time )); for (j = 0; f [j] <= I & j <= N; j ++) S [SG [I-f [j] = 1; // mark the SG function value in the subsequent State as for (j = 0; j ++) if (! S [j]) {// query the smallest non-zero value in the SG value of the current successor State. SG [I] = j; break ;}}}


Now we have a practical drill (question link ):

As long as you follow the above ideas, solving this problem is a matter of minutes.

The Code is as follows:

#include <stdio.h>#include <string.h>#define MAXN 1000 + 10#define N 20int f[N],SG[MAXN],S[MAXN];void getSG(int n){    int i,j;    memset(SG,0,sizeof(SG));    for(i = 1; i <= n; i++){        memset(S,0,sizeof(S));        for(j = 0; f[j] <= i && j <= N; j++)            S[SG[i-f[j]]] = 1;        for(j = 0;;j++) if(!S[j]){            SG[i] = j;            break;        }    }}int main(){    int n,m,k;    f[0] = f[1] = 1;    for(int i = 2; i <= 16; i++)        f[i] = f[i-1] + f[i-2];    getSG(1000);    while(scanf("%d%d%d",&m,&n,&k),m||n||k){        if(SG[n]^SG[m]^SG[k]) printf("Fibo\n");        else printf("Nacci\n");    }    return 0;}

If you are not satisfied yet, I am attaching some questions about the combined game to you:

POJ 2234 Matches Game
HOJ 2533 Stone II
POJ 2975 Nim
HOJ 1367 A Stone Game
POJ 2505 A multiplication game
ZJU 3057 beans game
POJ 1067 stone game
POJ 2484 A Funny Game
POJ 2425 A Chess Game
POJ 2960 S-Nim
POJ 1704 Georgia and Bob
POJ 1740 A New Stone Game
POJ 2068 Nim
POJ 3480 John
POJ 2348 Euclid's Game
HOJ 2645 WNim
POJ 3710 Christmas Game
POJ 3533 Light Switching Game

(If any error occurs, please correct it and repost it to indicate the source)




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.