hdu 4315 Climbing the Hill (阶梯博弈)

Climbing the Hill

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


Problem Description
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.
 

Author
TJU
 

Source
 

Recommend
zhuyuanchen520   |   We have carefully selected several similar problems for you:   4310  4311  4312  4313  4314 
 

Statistic |  Submit |  Discuss |  Note



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

题解:阶梯博弈

我们先考虑没有国王的情况。如果没有国王其实这道题就是和上一题poj1704等价的。因为我们假设最终变成了n个球相邻的情况,那么面临这个状态的人一定会输。为什么?因为先手无论怎么走,后手都有机会选择是否将当前状态留给先手。

我们先考虑分组,从后往前每两个分成一组,那么组内的空格数其实就相当于奇数堆的个数。因为如果先手使奇数堆的个数增加,那么后手一定有办法通过从奇数堆向前移动使其奇数堆中的个数不变。为什么只是奇数堆做nim呢?对手移动奇数堆的石子到偶数堆..我们跟着移动这些石子到下一个奇数堆...那么最后是对手把这些石子移动到了0..我们不能继续跟着移动...就只能去破坏原有的Nim而导致胜负关系的不确定。所以奇数堆的异或和直接决定了游戏的胜负。

但是我们现在牵扯到了国王的问题。如果k=1,那么Alice一定会赢。如果k=2且n为偶数的话的话需要特判,因为这样再从后往前两两分组的时候就会将第一个与空格子分成一组,如果我们再像刚才那样考虑变成n个相邻的话就相当于第一个球已经在空格子中了,那么当前面对的就是必胜态了,所以我们需要让第一个球移动到空格子的后一个表示这一组的个数。

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define N 1005

int n,k,cnt,ans;
int a[N],b[N];

int main()
{
    while (~scanf("%d%d",&n,&k))
    {
        ans=0; a[0]=0;
        for (int i=1;i<=n;++i) scanf("%d",&a[i]);
        if (k==1) {
            puts("Alice");
            continue;
        }
        if (k!=2) a[0]=-1;
        for (int i=1;i<=n;++i) b[n-i+1]=a[i]-a[i-1]-1;
        for (int i=1;i<=n;i+=2) ans^=b[i];
        if (!ans) puts("Bob");
        else puts("Alice");
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值