Hdu4111Alice and Bob(dp+博弈论)

Alice and Bob

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1540    Accepted Submission(s): 563


Problem Description
Alice and Bob are very smart guys and they like to play all kinds of games in their spare time. The most amazing thing is that they always find the best strategy, and that's why they feel bored again and again. They just invented a new game, as they usually did.
The rule of the new game is quite simple. At the beginning of the game, they write down N random positive integers, then they take turns (Alice first) to either:
1. Decrease a number by one.
2. Erase any two numbers and write down their sum.
Whenever a number is decreased to 0, it will be erased automatically. The game ends when all numbers are finally erased, and the one who cannot play in his(her) turn loses the game.
Here's the problem: Who will win the game if both use the best strategy? Find it out quickly, before they get bored of the game again!
 

Input
The first line contains an integer T(1 <= T <= 4000), indicating the number of test cases.
Each test case contains several lines.
The first line contains an integer N(1 <= N <= 50).
The next line contains N positive integers A 1 ....A N(1 <= A i <= 1000), represents the numbers they write down at the beginning of the game.
 

Output
For each test case in the input, print one line: "Case #X: Y", where X is the test case number (starting with 1) and Y is either "Alice" or "Bob".
 

Sample Input
  
  
3 3 1 1 2 2 3 4 3 2 3 5
 

Sample Output
  
  
Case #1: Alice Case #2: Bob Case #3: Bob
 
博弈真是博大精深的一门学问啊。后者由前者的状态可以得出结论。后者有多个前者,只要一者满足条件即可判断输赢。详细解题报告见:http://blog.csdn.net/ahero_happy/article/details/6955595 注意这个相当于打表,越到后面越省时间。状态都存放在sg数组中。(要改掉想题时抓头发的坏习惯。不记得某某某说过了程序猿都是秃子。突然看到草稿本上掉了很多毛发,原来如此。。。)


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <vector>
using namespace std;
#define LL __int64
int sg[60][60000];
int dp(int a,int b)
{
	if (sg[a][b]!=-1) 
		return sg[a][b];
	if (b==1) 
		return sg[a][b]=dp(a+1,0);
	sg[a][b]=0;
	if (a>0&&!dp(a-1,b))
		sg[a][b]=1;
	if (b>0&&!dp(a,b-1))
		sg[a][b]=1;
	if (a>0&&b&&!dp(a-1,b+1))
		sg[a][b]=1;
	if (a>1&&((b==0&&!dp(a-2,b+2))||(b&&!dp(a-2,b+3))))
		sg[a][b]=1;
	return sg[a][b];
}
int main()
{
	int T,n,i,x,y,z;
	memset(sg,-1,sizeof(sg));
	scanf("%d",&T);
	for (int c=1;c<=T;c++)
	{	
		y=0;z=0;
		scanf("%d",&n);
		for (i=1;i<=n;i++)
		{
			scanf("%d",&x);
			if (x==1)
				y++;
			else z+=x+1;
		}
		if (z) z--;
		dp(y,z);
		printf("Case #%d: ",c);
		if (sg[y][z]) puts("Alice");
		else puts("Bob");
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值