TJU训练赛—J

4172.   I-The dynamic programming problem1
Time Limit: 1.0 Seconds   Memory Limit: 65536K
Total Runs: 144   Accepted Runs: 30



Alice and Bob are playing a game. There is a line chess(black and white) in front of them. Firstly, one of them choose two continuous black chess, and replace them to a white chess. And then, next people do the same thing. The one who can not do anything is the loser. Alice is the first one. Now, could you tell me who will be the winner.

Input

The input consists of multiple test cases. The first line contains an integer  T T, indicating the number of test cases. (1T1000) (1≤T≤1000)
Each case contains one integer  N N. (1N1000) (1≤N≤1000). Then comes a sequence of length N with 0 or 1, 1 means black chess and 0 means white chess.

Output

For each case, if Alice wins, output"Alice", otherwise output"Bob". 

Sample Input

2
6
011110
1
1

Sample Output

Alice
Bob

【分析】

暴力求SG函数,所有连续的黑点看成一组单独的游戏,所有游戏的答案异或一下就是最终答案.

这里当前可改变的状态就是枚举取当前段中的任意相邻两个合并成一个白色点,合并后必然会使得当前连续的黑点变成两段连续的黑点,那么后继状态就是(sg[i-j-2]^sg[j])

关于SG函数:博弈入门—NIM&SG

【代码】

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

int sg[1100]={0};
int vis[1100];
char s[2000];
void init()  
{  
    sg[0]=sg[1]=0;  
    for (int i=2;i<=1000;i++)  
    {  
        memset(vis,0,sizeof(vis)); 
        for (int j=0;j<=i-2;j++)  
        	vis[sg[i-j-2]^sg[j]]=1;  
        for (int j=0;;j++)
            if (!vis[j])  
            {  
                sg[i]=j;  
                break;  
            }  
    }  
}  
int main()  
{
	init();
	int pp;scanf("%d",&pp);
	while (pp--)
	{
		int now=0,ans=0;
		int n;scanf("%d",&n);scanf("%s",s);
		for (int i=0;i<n;i++)
		{
			if (s[i]=='0')
			{
				ans^=sg[now];
				now=0;
			}
			else now++;
		}
		ans^=sg[now];
		if (ans) puts("Alice");
		else puts("Bob");
	}
    return 0;    
}  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值