博弈——Bash游戏 V2(SG 打表找规律)

18 篇文章 0 订阅

博弈——Bash游戏 V2(SG 打表找规律)

题目链接:
51Nod - 1067

有一堆石子共有N个。A B两个人轮流拿,A先拿。每次只能拿1,3,4颗,拿到最后1颗石子的人获胜。假设A B都非常聪明,拿石子的过程中不会出现失误。给出N,问最后谁能赢得比赛。

例如N = 2。A只能拿1颗,所以B可以拿到最后1颗石子。

Input

第1行:一个数T,表示后面用作输入测试的数的数量。(1 <= T <= 10000) 第2 - T + 1行:每行1个数N。(1 <= N <= 10^9)

Output

共T行,如果A获胜输出A,如果B获胜输出B。

Sample Input

3
2
3
4

Sample Output

B
A
A

分析

这题可以通过 SG 打表来寻找规律
在这里插入图片描述
从图中我们可以看出,当 n % 7 = 0 或 n % 7 = 2 时,先手必败,即 B 获胜,反之,A 获胜。

打表代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int S[100000],SG[100000],f[4]={0,1,3,4};

void getSG(int n)
{
	memset(SG,0,sizeof(SG));
	for(int i=1;i<=n;i++){
		memset(S,0,sizeof(S));
		for(int j=1;j<=3&&f[j]<=i;j++){
			S[SG[i-f[j]]]=1;
		}
		for(int t=0; ;t++){
			if(!S[t]){
				SG[i]=t;
				break;
			}
		}
		cout<<"i="<<i<<"      SG[i]="<<SG[i]<<endl;
	}
}

int main()
{
	getSG(100);
	return 0;
}

AC 代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
using namespace std;

int main()
{
	int t;
	cin>>t;
	while(t--){
		int n;
		cin>>n;
		if(n%7==0||n%7==2){
			cout<<"B"<<endl;
		}
		else{
			cout<<"A"<<endl;
		}
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值