POJ - 2960 S-Nim (SG)

题意:给你一个数组和n堆石子和每堆的石子数量,你每次只能拿数组中的数,问你怎样谁输谁赢

思路:这种套公式的SG还是挺简单的,就是打一个SG表,之后随便搞搞就好了,坑点是要对数组中的数先排序

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
const int maxnE = 10000 + 10;
const int maxnV = 10000;
using namespace std;
int a[maxnE],vis[maxnV],SG[maxnV];
int n,m,k;
void getsg()
{
//	SG[0] = 0;
	memset(SG,0,sizeof(SG));
	memset(vis,0,sizeof(vis));
	for(int i = 1 ; i <= maxnV;i++)//每堆的石子数量
	{
		memset(vis,0,sizeof(vis));
		for(int j = 0 ; j < n && i>=a[j]; j++) // 因为这里要让a从小到大开始才行, 
		{
			vis[SG[i-a[j]]] = 1; //当前石子i他的子游戏是i-a[i]:
		}
		for(int j = 0 ; ; j ++)
		{
			if(!vis[j])
			{
				SG[i] = j;
				break;
			}
		}
	}
}
int main()
{
	
	while(scanf("%d",&n)!=EOF)
	{
		if(n == 0) break;
		memset(a,0,sizeof(a));
		for(int i = 0 ; i < n ; i++)
		{
			scanf("%d",&a[i]);
		}
		sort(a,a+n);//先排序 
		getsg();
		int t,p;	
		scanf("%d",&t);
		while(t--)
		{
			int te;
			scanf("%d",&p);
			int ans = 0 ;
			for(int i = 0 ; i < p; i ++)
			{
				scanf("%d",&te);
				ans = ans ^ SG[te];
			}
			if(ans)printf("W");
			else printf("L");
		}
		puts("");
	}
}


猜你喜欢

转载自blog.csdn.net/wjmwsgj/article/details/80196987
今日推荐