C-a coloring game
Crawling in process...Crawling failedTime
Limit:500 msMemory limit:65536kb
64bit Io format:% I64d & % i64u
Submitstatus
Practice
Sgu 1, 328
Description
Two players play a graph coloring game. they make moves in turn, first player moves first. initially they take some undirected graph. at each move, a player can color an uncolored vertex with either white or black color (each player can use any color, possibly
Different at different turns). It's not allowed to color two adjacent vertices with the same color. A player that can't move loses.
After playing this game for some time, they decided to study it. For a start, they 've decided to study very simple kind of graph-a chain. A chain consistsNVertices,
V1,V2 ,...,
VN, AndN-1 edges, connecting
V1V2,
V2V3 ,...,
VN-1VN.
Given a position in this game, and assuming both players play optimally, who will win?
Input
The first line of input contains the integer
N,.
The second line of input describes the current position. It containsNDigits without spaces.IThDigit describes the color of vertexVI:
0-uncolored, 1-black, 2-white. No two vertices of the same color are adjacent.
Output
On the only line of output, print"
FIRST
"(Without quotes) if the player moving first in that position wins the game, and"
SECOND
"(Without quotes) otherwise.
Sample Input
sample input |
sample output |
500100 |
SECOND |
sample input |
sample output |
41020 |
FIRST |
Game SG Functions
I understood this question after the game.
First, the SG functions of this question can be summarized into four categories.
The first class: the SG function in the form of x00... 00y (x = Y) is 0.
Class 2: x00... 00y (X! = Y) the SG function in the form of this state is 1.
Category 3: The number of SG functions in the form of x00... 00 is zero. For example, the SG function of 1000 is 3.
Category 4: In the form of 0000... 000, if the number of zeros is an even number, the SG function is 0. Otherwise, the SG function is not equal to 0.
#include <cstdio>#include <cstring>using namespace std;char str[100010];int main() { int n; while (~scanf("%d", &n)) { scanf("%s", str); bool flag = true; for (int i = 0; i < n; i++) if (str[i] != '0') { flag = false; break; } if (flag) { if (n % 2 == 1) printf("FIRST\n"); else printf("SECOND\n"); continue; } int ans = 0; int p = 0; while (str[p] == '0') p++; if (p) ans ^= p; int q = n; while (str[q - 1] == '0') q--; if (q < n) ans ^= n - q; int pre = p, cnt = 0; for (int i = p + 1; i < q; i++) if (str[i] == '0') { cnt++; } else { if (cnt && str[i] == str[pre]) ans ^= 1; pre = i; cnt = 0; } if (ans) printf("FIRST\n"); else printf("SECOND\n"); } return 0;}