Climbing the Hill (阶梯博弈---尼姆博弈变形)

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? 
InputThere 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.OutputIf 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送到山顶。

这题分两种情况:

1,King在第一位的时候,Alice获胜 , 因为Alice先手可直接将King送到山顶。

2,King在第二位且人数n为奇数时,从后往前两两配对,那么第一个人最多只能停在a[1]上 , 因为如果第一个人爬到山顶,那么king就可以直接上山顶了,所以这题可以转化为 , 谁必须移动第一个人谁必败。

此时a[0] = 0; 例如 , a[1] = 2, a[0]=0 ,a[1]只能移动到1的位置 所以这个堆是a[0]-1,而如果King不在第二位的话 , 那么a[1]就可以直接移动到山顶了 , 这个时候就比King在第二位时的情况多出一个 , 因此令a[0] = -1 , 因为-(-1) 等于 +1.

下面代码:

#include <iostream>
#include <algorithm>
using namespace std;
void pr(int ans)
{
    if(ans)
        cout<<"Alice"<<endl;
    else
        cout<<"Bob"<<endl;
}
int main()
{
    int n,k;
    while(cin>>n>>k)
    {
        int a[1005],ans=0;
        for(int i=1; i<=n; i++)
            cin>>a[i];
        a[0]=0;
        if(k==1)
        {
            cout<<"Alice"<<endl;
            continue;
        }
        if(k!=2) a[0]=-1; //只有当n为奇数的时候才会涉及到a[0] 因为a[1]单成一堆 
        //意为正常0到a[1]这个堆就应该是a[1]-1的 但是k不在2的时候a[1]可以移动到0
        //比正常的堆多一个位置 所以要-(-1)=+1
        for(int i=n; i>=1; i-=2)
            ans=ans^(a[i]-a[i-1]-1);
        pr(ans);
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值