Climbing the Hill (HDU-4315)(阶梯博弈—尼姆博弈)

本文介绍了一个名为Climbing the Hill的游戏,其中Alice和Bob轮流移动棋子以将国王移动到山顶,目标是在双方最优策略下确定胜利者。文章探讨了游戏策略,包括两两分组和异或运算的应用,以及如何通过分析棋子位置来预测游戏结果。

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

Alice and Bob are playing a game called "Climbing the Hill". The game board consists of cells arranged vertically, as the figure below, while the top cell indicates the top of hill. There are several persons at different cells, and there is one special people, that is, the king. Two persons can't occupy the same cell, except the hilltop. 

At one move, the player can choose any person, who is not at the hilltop, to climb up any number of cells. But the person can't jump over another one which is 
above him. Alice and Bob move the persons alternatively, and the player who move the king to the hilltop will win. 

                                                                                        



Alice always move first. Assume they play optimally. Who will win the game? 

Input

There are several test cases. The first line of each test case contains two integers N and k (1 <= N <= 1000, 1 <= k <= N), indicating that there are N persons on the 
hill, and the king is the k-th nearest to the top. N different positive integers followed in the second line, indicating the positions of all persons. (The hilltop is No.0 cell, the cell below is No.1, and so on.) These N integers are ordered increasingly, more than 0 and less than 100000.

Output

If Alice can win, output "Alice". If not, output "Bob".

Sample Input

3 3
1 2 4
2 1
100 200

Sample Output

Bob
Alice

        
  

Hint

The figure illustrates the first test case. The gray cell indicates the hilltop. The circles indicate the persons, while the red one indicates the king. The first player Alice
can move the person on cell 1 or cell 4 one step up, but it is not allowed to move the person on cell 2.

 题意:在山上有n个人,每个人编号是1~n,这些位置只能同时被一个人占据,但是山顶可以同时被多个人占据,距离山顶第k近的是King,现在Alice和Bob开始向上送人,条件是不能跨越前面最近的人,问在Alice先手,双方最优的条件下谁能把King送到山顶。

思路:当k=1时,Alice必胜;

 当k!=1时,两两分组,如果前面只有一个球即n为奇数且k=2时,Alice和Bob都不愿移动前面的球到0,所以此时前面球有hill[1]-1种选择,所以hill[1]--。

对于这个题目,从后向前两两划分成一组,组内相当于奇数阶梯上的石子,组间相当于偶数阶梯上的石子,移动组内的前面石子一定能够通过移动当前组的后面石子相同步数达到平衡态,移动组内后面的石子一定能够通过移动其他组后面石子达到平衡态,如果这么考虑的话这题就是阶梯博弈。

本题是每两个点看成一个堆,n=1和k=2要单独讨论,每两个点相对于山顶运动,注意移动的时候山顶永远都算一个空格,异或所有堆,可以看做是尼姆博弈。

也可以将堆与堆之间看做是偶数步的点,也相当于只异或奇数堆,即阶梯博弈。

这个问题是谁能掌握主动权让对面去面对0的事,与k在组内前后无关。

1.假设n=5,k=2。1, 2,5, 6,10。 三堆是0,2,3。先手必胜。先手走成0,2,2让后手面对必败态,后手可以把局面走成0,0,0。

最后最后后手只能选择把第一个数走到0,先手把2移动到0胜。

2.假设n=5,k=4。1,2,5,6,10。三堆是1,2,3。异或为0先手必败。注意这组数据1是可以走到0的上组数据不可以。

3.假设n=4,k=2。1,3,4,7。两堆是1,2。异或不为0先手必胜。先手走成1,1即可。

注意当第一个数1走到0的时候,第二个数3走到的是2而不是1也不是0,1走到了0,3走到了2,中间距离还是1。

即相对运动不改变该堆的值,一定要注意3走到的是2而不是1也不是0。

AC代码:(Java)

public class Main {
	public static void main(String[] args) {
		Scanner in =new Scanner(System.in);
		int n,k;
		int a[]=new int[100010];
		while(in.hasNext())
		{
			n=in.nextInt();
			k=in.nextInt();
			int ans=0;
			for(int i=1;i<=n;i++)
				a[i]=in.nextInt();
			a[0]=0;
			if(k==1)
			{
				System.out.println("Alice");
				continue;
			}
			if(k!=2) a[0]=-1;
			for(int i=n;i>=1;i-=2)
             ans^=a[i]-a[i-1]-1;
			if(ans!=0)
				System.out.println("Alice");
			else
				System.out.println("Bob");
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值