POJ 2960 S-Nim

题意:在普通的Nim游戏上加入一些限制。给定一个集合S, 每次只能取S的元素个石子。

题解:SG函数。

#include <iostream>
using namespace std;

#define MAXN 105
#define MAXM 10005

int h[MAXN];
int s[MAXN];
int SG[MAXM];
int k;

void DFS ( int x )
{
	if ( SG[x] != -1 ) return;
	bool check[MAXM] = { 0 };

	int i, temp;
	for ( i = 1; i <= k; i++ )
	{
		temp = x - s[i];
		if ( temp >= 0 )
		{
			if ( SG[temp] == -1 )
				DFS ( temp );
			check[SG[temp]] = true;
		}
	}

	for ( i = 0; i <= MAXN; i++ )
		if ( ! check[i] ) break;
	SG[x] = i;
}

int main()
{
	int m, l, x, i;
	while ( scanf("%d",&k) )
	{
		if ( k == 0 ) break;
	    for ( i = 1; i <= k; i++ )
		    scanf("%d",&s[i]);	
		SG[0] = 0;
	    for ( i = 1; i <= MAXM; i++ ) SG[i] = -1;

		scanf("%d",&m);
		while ( m-- )
		{
			scanf("%d",&l);
			int res = -1;
			while ( l-- )
			{
				scanf("%d",&x);
				if ( SG[x] == -1 ) DFS ( x );
				if ( res == -1 ) res = SG[x];
				else res ^= SG[x];
			}
			
			if ( res ) printf("W");
			else printf("L");
		}
		printf("\n");
	}
	return 0;
}
	


你可能感兴趣的:(游戏)