--SG Template of Game

Source: Internet
Author: User

I haven't had a game in a long time. To write a template first:

Now let's look at a game that seems more general: given a piece with a direction-free graph and a starting vertex, the two players alternately move the piece along a forward edge and cannot be judged negative by the moving side. In fact, this game can be thought of as an abstract model of all impartial combinatorial games. In other words, any ICG can be abstracted into this " graph game "by taking each situation as a vertex and a forward edge to each situation and its sub-situation. Below we define the Sprague-garundy function on the vertex with the direction-free graph.

The Mex (minimal excludant) operation is defined first, which is an operation applied to a set 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, define the sprague-garundy function g for each vertex of the graph as follows:g (x) =mex{g (y) | y is the successor of x .

Consider the nature of the SG function. First, all the vertices of the terminal position , that is, the vertex without the edge, whose SG value is 0, because its successor is an empty set. Then for a g (x) =0 vertex x, all subsequent y of it satisfies the g (Y)!=0. For a vertex of a g (x)!=0 , there must be a successor y that satisfies the g (Y) =0.

The above three sentences indicate that vertex x represents the postion is p-position when and only if g (x) =0(with p-positioin/n-position the definition of that three sentence is exactly the same). We can find a winning strategy for each situation by calculating the SG value for each vertex that has a non-circular graph. But the use of the SG function is far less simple. If the game becomes a bit more complicated, for example, there is not only one chess piece on the map, but there are n pieces, each time you can choose one to move, then, how to find the winning strategy?

Let's take a look at the vertexSGThe meaning of the value. Wheng (x) =kis indicated for any one of the0<=i<k, there arexA successor ofyMeetg (Y) =i。 In other words, when a pieceSGValue iskWhen we can turn it into a0Become1、...Becomek-1, but absolutely cannot keepkNot change. I don't know if you can think of this.NimGameNimThe rules of the game are: each time you select a stack ofkStone, you can turn it into a0Become1、...Becomek-1, but absolutely cannot keepkNot change. This indicates that if theNThe vertices where the pieces are located.SGValues asNHeap the corresponding number of stones, then thisNimEach winning strategy of the game corresponds to the originalNA winning strategy for pieces!

For n pieces, the SG values for their corresponding vertices are (a1,a2,..., an), and the Nim at the time of the situation (A1,a2,..., an) is set. One of the game's winning strategy is to turn ai into k, then the original game is a winning strategy is to move the first piece to a SG value of K Vertex. It sounds a bit too magical - how to go around and back to the Nim game.

In fact, we just have to prove that this multi-piece of the graph game situation is p-position when and only if all pieces are located in the position of the SG function xor is 0. This proof is almost identical to the Bouton ' s theorem of the previous section, and only requires proper modification of several nouns.

Just now, I want to make the problem seem easier, think that the n pieces are moving on a graph. But if it is not on a map, but each piece on a graph, each time you can choose a piece (that is, any one of the graph) to move, so that the conclusion does not bring any changes.

So we can define a graph game and(Sum of Graph games): SetG1、G2、...、GnIsNA graph game, define the gameGIsG1、G2、...、Gnand the(Sum)GameGThe move rule is: Choose a sub-gameGiAnd move the pieces above.Sprague-grundy theoremIt is:g (g) =g (G1) ^g (G2) ^...^g (Gn)。 In other words, the game and theSGThe function value is all of its sub-gamesSGThe XOR of the value of the function.

Consider again at the beginning of this article: any ICG can be abstracted into a graph game. So the concept of "SG function " and " game and " is not confined to the graph game. We define a SG value for each position for each ICG , and we can define the and of n ICG . So when we're dealing with a game of N Games, we can all think of these SG values as Nim for each game by figuring out the SG value for each situation. Stone heap, and then according to find Nim 's winning strategy method to find the game's winning strategy!

There are n heap of stones, every time from the 1th heap of stones to take 1, 2 or 3, you can take an odd number from the 2nd heap of stones, can be from the 3rd heap and after the stone to take any of the ... We can think of it as 3 sub-games, the 1th sub-game only a heap of stones, each time can take 1, 2, 3, it is easy to see the X-stone situation of the SG value is x%4. The 2nd sub-game is also only a bunch of stones, each can take odd, after a simple drawing can know this game has x stone when the SG value is x%2. The 3rd game has n-2 Heap stone, is a NIM game. For each situation of the original game, the three sub-game of the SG value of different or a whole game of the SG value, and then can be based on the SG value to determine whether there is a winning strategy and make a decision. In fact, as 3 sub-game or conservative, simply as N sub-game, of which the 1th, 2 sub-games as described above, 3rd and later sub-game are "1 heap of stones, each take a few can", called "Take the Stone Game", this super simple game has x stone of the SG value is obviously x. In fact, n heap of Pebbles of the Nim game itself is not n "Take a stone game" and?

>>>>>>> Reprint Please specify the Source: Looking for Children & stars <<<<<<<

The above is not clear, to a simple digest it:

For a given, forward-free graph, define the Sprague-grundy function g for each vertex of the graph as follows: g (x) =mex{g (y) | Y is the successor of X, where G (x) is sg[x]

For example: Take the stone problem, there are 1 heap n stone, at a time can only take {1,3,4} a stone, first take the stone winner, then the number of the SG value?

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

X=1, you can take away 1-f{1} stones, the remaining {0}, mex{sg[0]}={0}, so sg[1]=1;

x=2, you can take away 2-f{1} stones, the remaining {1}, mex{sg[1]}={1}, so sg[2]=0;

X=3, you can take away 3-f{1,3} stones, the remaining {2,0}, mex{sg[2],sg[0]}={0,0}, so sg[3]=1;

X=4, you can take away 4-f{1,3,4} stones, the remaining {3,1,0}, mex{sg[3],sg[1],sg[0]}={1,1,0}, so sg[4]=2;

X=5, you can take away 5-f{1,3,4} stones, the remaining {4,2,1}, mex{sg[4],sg[2],sg[1]}={2,0,1}, so sg[5]=3;

And so on .....

x012345678....

SG[X] 010123201....



Calculates the SG value from the 1-n range.

F (the number of steps that can be walked, f[0] indicates how many ways to go)

F[] need to sort from small to large

1. The number of steps can be 1~m continuous integer, directly modulo, SG (x) = x (m+1);

2. The optional step is any step, SG (x) = x;

3. The optional number of steps is a series of discontinuous numbers, calculated with GETSG ()

Template 1 is as follows (SG Chart):

1 //f[]: The number of stones that can be taken away2 //sg[]:0~n value of SG function3 //hash[]:mex{}4 intF[n],sg[n],hash[n]; 5 voidGETSG (intN)6 {7     inti,j;8memset (SG,0,sizeof(SG));9      for(i=1; i<=n;i++)Ten     { Onememset (Hash,0,sizeof(hash)); A          for(j=1; f[j]<=i;j++) -hash[sg[i-f[j]]]=1; -          for(j=0; j<=n;j++)//The smallest non-negative integer that is not present in mes{} the         { -             if(hash[j]==0) -             { -sg[i]=J; +                  Break; -             } +         } A     } at}

Template 2 is as follows (DFS):

1 //Note that the S array to sort by from small to large the SG function is initialized to-1 for each collection simply initialize 1 times2 //N is the size of the set S S[i] is an array of special rules that are defined3 ints[ the],sg[10010],n;4 intSg_dfs (intx)5 {6     inti;7     if(sg[x]!=-1)8         returnSg[x];9     BOOLvis[ the];Tenmemset (Vis,0,sizeof(Vis)); One      for(i=0; i<n;i++) A     { -         if(x>=S[i]) -         { theSg_dfs (xs[i]); -vis[sg[x-s[i]]]=1; -         } -     } +     inte; -      for(i=0;; i++) +         if(!Vis[i]) A         { atE=i; -              Break; -         } -     returnsg[x]=e; -}


Exercise: hdu1848 && hdu1536

Problem Solving report:>>hdu1848<< && >>hdu1536<<

Like the game students are also welcome to my open topic to do together

Links: >> Tricks of the game <<

--SG Template of Game

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.