hdu4315 Climbing the Hill(阶梯博弈)

Climbing the Hill

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1218    Accepted Submission(s): 548

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

2012 Multi-University Training
Contest 2

题意】

有N个人爬山,山顶坐标为0,其他人的坐标按升序给出。不同的坐标只能容纳一个人(山顶不限),Alice和Bob轮流选择一个人让他移动任意步,但不能越过前面那个人。现在有一个人是king(给出id),谁能将king移动到山顶就算赢。

【分析】

考虑King的情况和上述版本几乎一致,只要把King当作普通人一样处理即可。

除了两种特殊情况:

1. 当King是第一个人时,Alice直接胜

2. 当King是第二个人且一共有奇数个人时,第一堆的大小需要减1。

因为如果king在奇数石子上,那么king前面的那一对石子k1,k2.

当对方把k1移到top时,我可以把k2移到top前的一个位置.

那么对于k2石子,对方如果碰了它,那么我肯定会把king移到top的.

所以k2也相当于是一颗废子而已,不影响最终Nim的结果.

Ps:阶梯博弈学习博客链接:阶梯博弈

#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
const double eps = 1e-6;
const double pi = acos(-1.0);
const int INF = 1e9;
const int MOD = 1e9+7;
#define ll long long
#define CL(a,b) memset(a,b,sizeof(a))
#define lson (i<<1)
#define rson ((i<<1)|1)
#define N 50010
int gcd(int a,int b){return b?gcd(b,a%b):a;}

int n,k;
int s[1010];

int main()
{
    while(scanf("%d%d",&n,&k)==2)
    {
        for(int i=1; i<=n; i++)
            scanf("%d",&s[i]);
        if(k == 1) {cout<<"Alice"<<endl; continue;}
        int ans;
        if(n%2 == 0)
        {
            ans = s[2]-s[1]-1;
            for(int i=4; i<=n; i+=2) ans ^= (s[i]-s[i-1]-1);
            if(ans == 0) cout<<"Bob"<<endl;
            else cout<<"Alice"<<endl;
        }
        else
        {
            ans = s[1];
            if(k == 2) ans -= 1;
            for(int i=3; i<=n; i+=2) ans ^= (s[i]-s[i-1]-1);

            if(ans == 0) cout<<"Bob"<<endl;
            else cout<<"Alice"<<endl;
        }
    }
    return 0;
}
时间: 2024-12-13 10:00:18

hdu4315 Climbing the Hill(阶梯博弈)的相关文章

HDU4315 Climbing the Hill

题目链接:https://cn.vjudge.net/problem/HDU-4315 知识点: 博弈论 题目大意: \(Alice\) 和 \(Bob\) 轮流指挥 \(N\) 个人爬山,这 \(N\)个人在山顶下的不同层,国王是第 \(k\) 个人.山的每一层都最多只能容纳 \(1\) 个人(除了山顶),两个玩家每次都能指挥任意一个人向上爬任意层直到山顶,但不能让一个人越过另一个人.指挥国王爬到山顶上即可获胜. 解题思路: 首先,如果国王是第一个人,先手必胜. 如果 \(N\) 是偶数,关键

HDU 4315 Climbing the Hill [阶梯Nim]

传送门 题意: 和上题基本一样:山顶可以有多人,谁先把king放到山顶谁就胜 并不太明白 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; typedef long long ll; const int N=1005; inline int read(){ char

HDU 4315:Climbing the Hill(阶梯博弈)

http://acm.hdu.edu.cn/showproblem.php?pid=4315 题意:有n个人要往坐标为0的地方移动,他们分别有一个位置a[i],其中最靠近0的第k个人是king,移动的时候在后面的人不能越过前面的人,先把king送到0的人胜. 思路:阶梯博弈.把n个人两两配对,形成一个组,即a[i]和a[i+1]是一个组,a[i+2]和a[i+3]是一个组,把a[i]和a[i+1]的距离当成阶梯博弈中的奇数阶的值,a[i+1]和a[i+2]的距离当成偶数阶(不用考虑).首先k=1

HDU 4315 Climbing the Hill (尼姆博弈)

Climbing the Hill Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status Description Alice and Bob are playing a game called "Climbing the Hill". The game board consists of cells arranged vertically, as the

Codeforces 812E Sagheer and Apple Tree ——(阶梯博弈)

之前在bc上做过一道类似的阶梯博弈的题目,那题是移动到根,这题是移动到叶子.换汤不换药,只要和终态不同奇偶的那些位置做nim即可.因此这题给出了一个条件:所有叶子深度的奇偶性相同.同时需要注意的是,上次bc中,根节点是不能移动的,因此根节点是终态节点,而这里叶子上面还可以进行操作(可以吃掉),那么就相当于叶子节点都还可以继续向下移动,因此他们不是终态节点,也就是说这题只要和叶子节点同奇偶的做nim即可. 因此,如果nim和已经是0,已经可以满足先手必输了,而题目说了必须要交换,那么只要让奇偶性相

阶梯博弈

由于bestcoder的一道题,去学习了一下阶梯博弈. 大概理解如下:有n层的阶梯,每一层上都有若干的石子,可以将任何一层的石子,拿出至少一个,放到它的上一层上去,最后一个不能进行操作的人输. 那么,必胜策略是怎么样的呢?首先,我们令最高层为0层,依次为1,2,...,n-1层.那么,结论就是奇数层的石子做nim即可. 为什么这样是可行的呢?解释如下:对于偶数层,2,4,6...等,从这里移动石子,下一个人可以模仿着这动作将这么多的石子移动到下一层,例如,对方移动x个石子从2到1,那么我模仿他将

hdu 3389 阶梯博弈

题意:1-N带编号的盒子,当编号满足A>B && A非空 && (A + B) % 3 == 0 && (A + B) % 2 == 1则可以从A中取任意石头到B中,谁不能取了谁就输.Alice先手 阶梯博弈:博弈在一列阶梯上进行,每个阶梯上放着自然数个点,两个人进行阶梯博弈,每一步则是将一个集体上的若干个点 ( >=1 )移到前面去,最后没有点可以移动的人输. 在本题中 1,3,4 的状态不能转移到其他状态; 其他每个状态皆可转移; 且位置特定

阶梯博弈(没怎么搞懂)

首先是对阶梯博弈的阐述...博弈在一列阶梯上进行...每个阶梯上放着自然数个点..两个人进行阶梯博弈...每一步则是将一个集体上的若干个点( >=1 )移到前面去..最后没有点可以移动的人输.. 如这就是一个阶梯博弈的初始状态 2 1 3 2 4 ... 只能把后面的点往前面放...如何来分析这个问题呢...其实阶梯博弈经过转换可以变为Nim..把所有奇数阶梯看成N堆石子..做nim..把石子从奇数堆移动到偶数堆可以理解为拿走石子..就相当于几个奇数堆的石子在做Nim..( 如所给样例..2^3

POJ 1704 Georgia and Bob(阶梯博弈+证明)

POJ 1704 题目链接 关于阶梯博弈有如下定理: 将所有奇数阶梯看作n堆石头,做Nim,将石头从奇数堆移动到偶数堆看作取走石头,同样地,异或值不为0(利己态)时,先手必胜. 定理证明看此博:http://blog.csdn.net/kk303/article/details/6692506 以下是POJ 1704的AC代码: //棋子只能往左走(最左有界线),可以走任意多格(>=1) //而且棋子不能越过在它前面的棋子(它左边的棋子) //每个格最多放一个棋子,说明棋子也不能走到另一个棋子所