题意概括:你已经赢得两局,最后一局是N个棋子往后移动,最后一个无法移动的玩家失败。
题目分析:有向无环图sg值游戏,尼姆游戏的抽象表达。得到每个棋子的sg值之后,把他们异或起来,考察异或值是否为0.
#include "cstdio"
int figure[][];
int sg[];
int t;
void fbegin()
{
for (int i=;i<;i++)
{
sg[i] = -;
for (int j=;j<;j++)
{
figure[i][j] = -;
}
}
}
int dfs(int n)
{
if (sg[n] != -)
return sg[n];
int hash[] = {};
for (int i=;i<t;i++)
{
if (figure[n][i] == )
hash[dfs(i)] = ;
}
for (int i=;;i++)
if (hash[i] == )
return sg[n] = i;
}
int main()
{
int a,b,sum;
while (scanf ("%d",&t) != EOF)
{
fbegin();
for (int i=;i<t;i++)
{
scanf ("%d",&a);
if (!a)
sg[i] = ;
while (a-- && scanf ("%d",&b))
{
figure[i][b] = ;
}
}
while (scanf ("%d",&a) && a)
{
sum = ;
while (a--)
{
scanf ("%d",&b);
sum ^= dfs(b);
}
printf ("%s\n",sum?"WIN":"LOSE");
} }
return ;
}