Summary of game Issues in SG function game

Source: Internet
Author: User

SG function: Given a directed acyclic graph and a piece of chess on the starting vertex, the two players move the piece along the directed edge alternately, and cannot determine the negative operator. In fact, this game can be considered as an abstract model of all Impartial Combinatorial Games. That is to say, any ICG can regard each situation as a vertex, and abstract each situation and its sub-situation into a directed graph game by a directed edge ". Next we define the SD-Garundy function on the vertex of the directed acyclic graph. 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. In my understanding, the sg function is a process of directed acyclic graph dfs. When dealing with nim games, multiple stone stacks can be seen as the exclusive or of multiple sg function values. Example: POJ2311 Cutting Game is a typical sg Game. Find the successor status. The question is to give a n * m piece of paper, each cut into two parts, who first cut to 1*1 to win. This is a successor question. The two parts cut each time are the successor of the previous State, as long as the sg values of the two parts are the sg values of the previous state. [Cpp] # include <iostream> # include <cstdio> # include <algorithm> # include <cstring> # include <string> # include <cmath> # include <set> # include <vector> # include <stack> # define mem (, b) memset (a, B, sizeof (a) # define FOR (a, B, I) for (I = a; I <= B; ++ I) # define For (a, B, I) for (I = a; I <B ;+++ I) # define N 1000000007 using namespace std; inline void RD (int & ret) {char c; do {c = getchar () ;}while (c <'0' | c> '9'); ret = c-'0 '; While (c = getchar ()> = '0' & c <= '9') {ret = ret * 10 + (c-'0 ');}} inline void OT (int a) {if (a >= 10) {OT (a/10);} putchar (a % 10 + '0 ');} int sg [201] [201]; int dfs (int a, int B) // general syntax of the sg function {if (sg [a] [B]> = 0) {return sg [a] [B];} int I, map [201], r; // The map tag array must be defined within dfs, otherwise, the mem (map, 0); FOR (2, (a/2), I) {r = dfs (I, B) ^ dfs (a-I, b); // subsequent variance or obtain the sg value map [r] = 1;} FOR (2, (B/2), I) of the previous State) {r = dfs (a, I) ^ dfs (a, B-I); map [r] = 1;} (0,200, I) {if (map [I] = 0) {return sg [a] [B] = I; // application of the mex formula }}int main () {int n, m, sum; mem (sg,-1); while (scanf ("% d ", & n, & m )! = EOF) {sum = dfs (n, m); if (sum> 0) {printf ("WIN \ n ");} else {printf ("LOSE \ n") ;}} return 0;} POJ2425 A Chess Game, the two players move the pawns in turn, and the players cannot move or lose. The typical sg function is used. Just apply the template. This question has a large amount of data. After the input is optimized, it is written to the fourth place in the first version, nice! [Cpp] # include <iostream> # include <cstdio> # include <algorithm> # include <cstring> # include <string> # include <cmath> # include <set> # include <vector> # include <stack> # define mem (, b) memset (a, B, sizeof (a) # define FOR (a, B, I) for (I = a; I <= B; ++ I) # define For (a, B, I) for (I = a; I <B ;+++ I) # define N 1000000007 using namespace std; inline void RD (int & ret) {char c; do {c = getchar () ;}while (c <'0' | c> '9'); ret = c-'0 '; While (c = getchar ()> = '0' & c <= '9') {ret = ret * 10 + (c-'0 ');}} inline void OT (int a) {if (a >= 10) {OT (a/10);} putchar (a % 10 + '0 ');} int vis [1001] [1001], sg [1001]; int n; int dfs (int x) // typical sg process {int I; if (sg [x]! =-1) {return sg [x];} int f [1001]; mem (f, 0); For (0, n, I) {if (vis [x] [I]! =-1) {f [dfs (I)] = 1 ;}} I = 0; while (f [I]) {I ++ ;} return sg [x] = I;} int main () {int I, j, k, t, x, p, sum; while (scanf ("% d ", & n )! = EOF) {mem (vis,-1); mem (sg,-1); For (0, n, I) {RD (k ); if (k = 0) {sg [I] = 0;} For (0, k, j) {RD (t); vis [I] [t] = 1; // graph creation} while (1) {RD (x); if (x = 0) {break;} sum = 0; For (0, x, I) {RD (p); sum ^ = dfs (p);} if (sum! = 0) {printf ("WIN \ n");} else {printf ("LOSE \ n") ;}} return 0 ;} POJ2068 Nim: There are 2n people on the Round Table, an odd number and an even number. Each person has a maximum number of chess pieces to take and ask if you can win the last one. The powerful sg function is used to record the sg in each State. [Cpp] # include <iostream> # include <cstdio> # include <algorithm> # include <cstring> # include <string> # include <cmath> # include <set> # include <vector> # include <stack> # include <queue> # include <map> # define mem (, b) memset (a, B, sizeof (a) # define FOR (a, B, I) for (I = a; I <= B; ++ I) # define For (a, B, I) for (I = a; I <B; ++ I) using namespace std; inline void RD (int & ret) {char c; do {c = getchar () ;}while (c <'0' | c> '9'); r Et = c-'0'; while (c = getchar ()> = '0' & c <= '9 ') {ret = ret * 10 + (c-'0') ;}} inline void OT (int a) {if (a> = 10) {OT (a/10);} putchar (a % 10 + '0');} int n, s, m [22], sg [22] [8200], sum; int dfs (int x, int y) {if (sg [x] [y]! =-1) {return sg [x] [y];} int I, j; FOR (1, m [x], I) {if (y-I <0) {break;} if (x + 1> = 2 * n) {j = 0;} else {j = x + 1;} if (dfs (j, y-I) = 0) {return sg [x] [y] = 1 ;}} return sg [x] [y] = 0 ;}int main () {int I; while (1) {RD (n); if (n = 0) {break;} RD (s); For (0, 2 * n, I) {RD (m [I]);} mem (sg,-1); FOR (0, 2 * n, I) {sg [I] [0] = 1 ;} sum = dfs (0, s); if (sum = 0) {printf ("0 \ n");} else {printf ("1 \ n ");}} return 0;} POJ3537 Cros Ses and Crosses: Give a rectangle of 1 * n with n squares on it. Now, the two draw X on it respectively. Who can draw three X pairs first to win. This is the parent Problem of a sg function into sub-problems, because in the x position after the x, it is converted into (X-3) grid painting x and (n-x-2)... This will be able to continue to break down, and finally all the sg values are the correct solution. [Cpp] # include <iostream> # include <cstdio> # include <algorithm> # include <cstring> # include <string> # include <cmath> # include <set> # include <vector> # include <stack> # include <queue> # include <map> # define mem (, b) memset (a, B, sizeof (a) # define FOR (a, B, I) for (I = a; I <= B; ++ I) # define For (a, B, I) for (I = a; I <B; ++ I) using namespace std; inline void RD (int & ret) {char c; do {c = getchar () ;}while (c <'0' | c> '9'); r Et = c-'0'; while (c = getchar ()> = '0' & c <= '9 ') {ret = ret * 10 + (c-'0') ;}} inline void OT (int a) {if (a> = 10) {OT (a/10);} putchar (a % 10 + '0');} int sg [2001]; int dfs (int x) {if (x <0) {return 0 ;}if (sg [x] >=0) {return sg [x] ;}int I, y; bool v [2001] ={ false }; FOR (1, x, I) {y = dfs (I-3) ^ dfs (x-i-2); // find successor (Classic) v [y] = true ;} for (I = 0; I ++) {if (v [I] = false) {return sg [x] = I ;}} int main () {int n, sum; mem (sg ,- 1); while (scanf ("% d", & n )! = EOF) {sum = dfs (n); if (sum) {printf ("1 \ n");} else {printf ("2 \ n ");}} return 0;} POJ2599 A funny game is A memory-based search. The game of this question is not strong, and more is A search. The question is to give a picture. The two move in turn, and the nodes that walk through cannot go any more. Just mark dfs +. [Cpp] # include <iostream> # include <cstdio> # include <algorithm> # include <cstring> # include <string> # include <cmath> # include <set> # include <vector> # include <stack> # define mem (, b) memset (a, B, sizeof (a) # define FOR (a, B, I) for (I = a; I <= B; ++ I) # define For (a, B, I) for (I = a; I <B ;+++ I) # define N 1000000007 using namespace std; inline void RD (int & ret) {char c; do {c = getchar () ;}while (c <'0' | c> '9'); ret = c-'0 '; While (c = getchar ()> = '0' & c <= '9') {ret = ret * 10 + (c-'0 ');}} inline void OT (int a) {if (a> = 10) {OT (a/10);} putchar (a % 10 + '0');} int n, v [1001] [1001], vis [1001]; int dfs (int x) {int I; FOR (1, n, I) {vis [x] = 1; if (v [I] [x] &! Vis [I]) {if (! Dfs (I) {vis [x] = 0; return I ;}} vis [x] = 0;} return 0 ;}int main () {int m, I, a, B; while (scanf ("% d", & n, & m )! = EOF) {mem (v, 0); mem (vis, 0); FOR (1, n-1, I) {RD (a); RD (B ); v [a] [B] = v [B] [a] = 1;} I = dfs (m); if (I! = 0) {printf ("First player wins flying to airport % d \ n", I);} else {printf ("First player loses \ n ");}} return 0 ;}

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.