首页 > 代码库 > POJ 2068 Nim 组合游戏

POJ 2068 Nim 组合游戏

题目大意:有一堆石子,两伙人,围在一起坐,坐的顺序是ABABABAB。。。。每一个人最多能取a[i]个石子,取走最后一个石子的就输了。问谁能赢。


思路:朴素的组合游戏判定问题,这个题给了数据范围,可以进行记忆化搜索。f[i][j]为还剩下i个石子,到了第j个人的时候的状态,然后记忆化一下。


CODE:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAX 10010
using namespace std;

int cnt,total,src[30];
int f[MAX][30];

bool Judge(int last,int pos)
{
	if(f[last][pos] != -1)	return f[last][pos];
	if(!last)	return true;
	int next = (pos + 1 > cnt * 2) ? 1:pos + 1;
	for(int i = 1; i <= src[pos] && last - i >= 0; ++i)
		if(!Judge(last - i,next))
			return f[last][pos] = 1;
	return f[last][pos] = 0;
}

int main()
{
	while(scanf("%d",&cnt) && cnt) {
		scanf("%d",&total);
		memset(f,-1,sizeof(f));
		for(int i = 1; i <= (cnt << 1); ++i)
			scanf("%d",&src[i]);
		printf("%d\n",Judge(total,1) ? 1:0);
	}
	return 0;
}


POJ 2068 Nim 组合游戏