hdu 4315 Climbing the Hill 博弈

来源:互联网 发布:java urlencoder utf8 编辑:程序博客网 时间:2024/04/29 15:10

建议做这道题目之前先看一下--------->>>点击打开链接

题意:

有 n 个球分别在 n 个不同的位置,Alice 和 Bob 依次选择一个球向上移动,上面有球不能越过,谁最后把红球移出谁就赢!



博弈问题,先考虑如果总数n为偶数(k!=1),当n个点全两两挨在一起时,谁先走谁输 

举个例子如果  

n==4   k==3 那么 先手怎么走你怎么走即可,你只需要把第2堆棋子在要进入山顶的时候把他放在靠近山顶一步的位置。那么相当于这一步给了对方,剩下的还是对方走什么你走什么就可以。对两两之间的配对异或就行。

n==4  k==4这样的话两两配对,对方走第一个你走第二个就可以。对两两之间的配对异或就行。

  当总数n为奇数时(k!=1),先吧第一个点走道终点,然后就是偶数的情况了,然后考虑怎么将他们两两挨在一起(注意不用所有点都挨在一起)

  这是就是当n为偶(k!=1)计算a[i+1]与a[i]的距离,for(i+=2),这样我们就将它化成几点nim游戏问题,将a[i+1]与a[i]的距离左为每堆石子的个数

  当n为奇数,就是a自己当作一个堆,然后还是将a[i+1]与a[i]的距离左为每堆石子的个数,轮到谁没有石子取了,谁就输啦!当然注意特殊情况就是当n为奇数的时候,k如果为2,前面我们说当n为奇数是直接将第一个直接走到终点,但因为第二哥是k了,所以第一个a[i]不能走道终点,只能走道里终点最近的位置也就是只能走a[i]-1步,所以就是将a[i]-1当作一个堆。


考虑了好长时间为什么k对整个游戏无影响,其实在第一个的时候就模拟了。这个问题解决了。再来考虑n的奇偶问题,这里有个特殊的例子,就是当n为奇数k==2.看了别人的博客没看懂,后个人认为为什么要考虑这个地方呢,主要是因为当k==1的时候肯定先手赢所以当k==2时,第一堆是不能全走完的否则后手就赢定了。。。也可以不减去一个直接进行全部异或就行但是要特判一下。

真的好神奇,,,看了好长时间才勉强接受。。。。

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 31 2 42 1100 200
 

Sample Output

BobAlice

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.          
 

这个是减去了不能走的部分

#include<iostream>#include<cstring>#include<cstdio>#define MAXN 1010using namespace std;int a[MAXN];int main(){    int n,k,i;    while(cin>>n>>k)    {        memset(a,0,sizeof(a));        for(i=1;i<=n;i++) scanf("%d",&a[i]);        if(k==1)        {            cout<<"Alice"<<endl;            continue;        }        int t;        if(n%2==0)        {            t=a[2]-a[1]-1;            for(i=4;i<=n;i=i+2) t=t^(a[i]-a[i-1]-1);            if(t==0)cout<<"Bob"<<endl;            else cout<<"Alice"<<endl;        }        else        {            t=a[1];            if(k==2) t-=1;            for(i=3;i<=n;i=i+2) t=t^(a[i]-a[i-1]-1);            if(t==0)cout<<"Bob"<<endl;            else cout<<"Alice"<<endl;        }    }    return 0;}

这个是对特殊情况特判了一下

#include<iostream>#include<cstring>#include<cstdio>#define MAXN 1010using namespace std;int a[MAXN];int main(){    int n,k,i;    while(cin>>n>>k)    {        memset(a,0,sizeof(a));        for(i=1; i<=n; i++) scanf("%d",&a[i]);        if(k==1)        {            cout<<"Alice"<<endl;            continue;        }        int t;        if(n%2==0)        {            t=a[2]-a[1]-1;            for(i=4; i<=n; i=i+2) t=t^(a[i]-a[i-1]-1);            if(t==0)cout<<"Bob"<<endl;            else cout<<"Alice"<<endl;        }        else        {            t=a[1];            for(i=3; i<=n; i=i+2) t=t^(a[i]-a[i-1]-1);            if(k==2)            {                if(t==0) cout<<"Alice"<<endl;                else cout<<"Bob"<<endl;            }            else            {                if(t==0)cout<<"Bob"<<endl;                else cout<<"Alice"<<endl;            }        }    }    return 0;}


0 0
原创粉丝点击