【hdoj 4315】Climbing the Hill

Climbing the Hill
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1043 Accepted Submission(s): 461

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

題意:兩個人移動棋子,其中有一個特殊棋子,移動的規則是棋子每次向上移動的格數無限制,但不能越過前面的棋子,誰先把 特殊的棋子移到最上面就算贏。
思路:1、確定必勝態和必敗態
2、分奇偶討論
3、特殊情況,當k==1時,先手必勝。
4、n%2==0時,(a1,a2),(a3,a4),………,(a(n-1),an),第i個棋子是和i+1個棋子相鄰的,i爲奇數。
當面對這個狀態時,如果特殊的棋子在偶數位置上,則後手必勝。如果特殊棋子在奇數位置,只需把i-1移動到最前面
就變成的偶數位置的情況。所以情況與特殊棋子位置無關。
n%2!=0時,(a1),(a2,a3),(a4,a5),………,(a(n-1),an),當特殊棋子在第一個位置爲必勝態,當特殊棋子在第二個位
置爲必敗態,先手取完a1就變成偶數的情況。
code:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int a[100015];
int n,k;
int main()
{
    while(scanf("%d%d",&n,&k)!=EOF&&n)
    {
        int t;
        memset(a,0,sizeof(a));
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
        }

        if(k==1)
            printf("Alice\n");
        else
        {
            if(n%2==0){
                t=a[2]-a[1]-1;
                for(int i=4;i<=n;i+=2)
                    t^=(a[i]-a[i-1]-1);
            }
            else{
                t=a[1];
                if(k==2)
                    t--;
                for(int i=3;i<=n;i+=2)
                    t^=(a[i]-a[i-1]-1);
            }
            if(t)
                printf("Alice\n");
            else
                printf("Bob\n");
        }



    }
    return 0;
}
發佈了68 篇原創文章 · 獲贊 9 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章