Crosses and Crosses POJ - 3537 (博弈论)

Crosses and Crosses POJ - 3537 (博弈论)

题意

给出 1 ∗ n 1*n 1n的棋盘,a,b轮流画×,完成三连×的获胜。

思路

当一个点被画下了×,那么作为当前棋手在它的左右各两格内下棋必然是输的,所以,一旦一点被下了后,游戏便被分解为两个游戏。设当前点为x,则两个分解游戏分别为 1 → ( x − 3 ) 1\rightarrow(x-3) 1(x3) ( x + 3 ) → n (x+3)\rightarrow n (x+3)n,即为 n 1 = ( x − 3 ) − 1 + 1 = x − 3 n_1=(x-3)-1+1=x-3 n1=(x3)1+1=x3 n 2 = n − ( x + 3 ) + 1 = n − x − 2 n_2=n-(x+3)+1=n-x-2 n2=n(x+3)+1=nx2的游戏。

AC代码

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<string>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<map>
#include<set>
using namespace std;
const long long inf = 0x3f3f3f3f;
#define ll long long
const int MAXN = 10005;
const double eps = 1e-6;
const ll MOD = 998244353;
int n, sg[2005];
int getSG(int x)
{
	if (x <= 0)
		return 0;
	if (sg[x] != -1)
		return sg[x];
	bool s[2005];
	memset(s, 0, sizeof(s));
	for (int i = 1; i <= x; i++)
	{
		s[getSG(i - 3) ^ getSG(x - i - 2)] = 1;
	}
	for (int i = 0;; i++)
		if (!s[i])
			return sg[x] = i;
}
int main()
{
	memset(sg, -1, sizeof(sg));
	while (cin >> n)
	{
		int ans = getSG(n);
		if (ans)
			cout << 1 << endl;
		else
			cout << 2 << endl;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值