Climbing the Hill HDU - 4315
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.
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?
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?
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.
3 3 1 2 4 2 1 100 200
Bob
Alice
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.
题意:Alice和Bob玩一个游戏,两人轮流从图中选择一个圆往上移任意格,但是不能越过其它圆,灰色的格子是终点,其他的格子不允许两个圆重叠,谁先把红圆移到终点谁就赢了。
思路:这道题可以从下往上把每两个圆分成一组,组内两圆的间隔可以看成奇数阶层的石子数,组间的间隔可以看成偶数阶层的石子数,因为如果移动组内前面的圆,那么下一轮可以移动组内后面的圆同样的距离保持平衡态,如果移动组内后面的圆,那么下一轮可以移动其它组后面的圆,这样就变成了阶梯博弈。有几个特殊情况,当k为1的时候一定是先手必胜,
当n为奇数k为2的时候,第一个圆没人愿意移动到终点,所以最终态提前了一格,所以ans要减一。
阶梯博弈详解:http://blog.youkuaiyun.com/shahdza/article/details/7858032
AC代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
#include <set>
#include <functional>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int INF = 1e9 + 5;
const int MAXN = 300005;
const int MOD = 1000000007;
const double eps = 1e-8;
const double PI = acos(-1.0);
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;
}