HDOJ 题目3389 Game(阶梯博弈)

本文介绍了一个游戏中的胜负预测算法,游戏中两名玩家轮流从编号盒子中按特定规则转移卡片,最终无法行动者为败。文章提供了AC代码实现,并通过实例演示了如何将问题转化为阶梯状尼姆博弈(Staircase Nim),利用异或运算找到获胜方。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Game

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


Problem Description
Bob and Alice are playing a new game. There are n boxes which have been numbered from 1 to n. Each box is either empty or contains several cards. Bob and Alice move the cards in turn. In each turn the corresponding player should choose a non-empty box A and choose another box B that B<A && (A+B)%2=1 && (A+B)%3=0. Then, take an arbitrary number (but not zero) of cards from box A to box B. The last one who can do a legal move wins. Alice is the first player. Please predict who will win the game.
 

Input
The first line contains an integer T (T<=100) indicating the number of test cases. The first line of each test case contains an integer n (1<=n<=10000). The second line has n integers which will not be bigger than 100. The i-th integer indicates the number of cards in the i-th box.
 

Output
For each test case, print the case number and the winner's name in a single line. Follow the format of the sample output.
 

Sample Input
  
2 2 1 2 7 1 3 3 2 2 1 2
 

Sample Output
  
Case 1: Alice Case 2: Bob
 

Author
hanshuai@whu
 

Source
 

Recommend
notonlysuccess   |   We have carefully selected several similar problems for you:   3387  3388  3390  3391  3393 
 

  1. 题意:有n个盒子编号为1-n,每个盒子里面有若干物品 
  2.       当编号满足a>b && (a+b)%3==0 && (a+b)%2==1时 
  3.       可以从a盒子中拿>=1个物品到b盒子中 
  4.  
  5. 思路:找规律后转换为Staircase Nim 
  6.       满足此条件:(a+b)%3==0 && (a+b)%2==1的即是3个倍数并且是奇数:3、9、15、21,即%6==3 
  7.       每六个拆开后会发现如下3组可以传递的关系: 
  8.       1<--2<--7<--8<--13<--14 
  9.       3<--6<--9<--12<--15<--18 
  10.       4<--5<--10<--11<--16<--17 
  11.       即3组Staircase Nim,3组结果异或即可。 

虽然似懂非懂,先珍藏一下再说吧

ac代码

#include<stdio.h>
int main()
{
	int t,c=0;
	scanf("%d",&t);
	while(t--)
	{
		int n,i,num[100000],s1=0,s2=0,s3=0,s;
		scanf("%d",&n);
		for(i=1;i<=n;i++)
			scanf("%d",&num[i]);
		for(i=2;i<=n;i+=6)
			s1^=num[i];
		for(i=6;i<=n;i+=6)
			s2^=num[i];
		for(i=5;i<=n;i+=6)
			s3^=num[i];
		s=s1^s2^s3;
		if(s)
			printf("Case %d: Alice\n",++c);
		else
			printf("Case %d: Bob\n",++c);
	}
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值