标签:
Description
Time Limit:1000ms Memory Limit:65536KB |
Description |
Nim is a game in which two players take turns removing stones from heaps. On each turn, a player must choose a single heap and remove one or more stones from that heap. The player who takes the last stone wins. Alice and Bob are bored with playing Nim over and over again, so they‘ve decided to create a new variation called Ordered Nim. Ordered Nim differs from regular Nim in the following way. The heaps are numbered 0 through n-1 (where n is the number of heaps), and a player can only remove stones from a heap if all the lower-numbered heaps are empty. You are given n interger(s), where the i-th interger(0-indexed) is the number of stones in heap i at the beginning of the game. Alice will take the first turn. Determine who will win the game, assuming both players play optimally. |
Input
input consist of multiple cases;process till EOF. each case contain two lines. on the first line is a interger n (1<=n<=50). the next line contain n interger(s),each of which will be between 1 and 1000000000,inclusive. |
Output
for each case output one line . if Alice can win print "Alice". otherwise print "Bob". |
Sample Input
1 5 2 1 2 |
Sample Output
Alice Bob |
Hint |
Source
srm450 #include<cstdio> int main() { int n; while(scanf("%d",&n)!=-1) { int cnt=0,leap=0,k; //查找序列中第一个大于1的数t,谁拥有这个数的主动权谁就赢 for(int i=1; i<=n; i++) { scanf("%d",&k); if(k>1) leap=1; if(!leap) cnt++; } //如果t前面的1的个数为奇数&&该序列不全为1(leap==1),Bob有主动权 if(cnt%2==1&&leap) printf("Bob\n"); else if(cnt%2==0&&leap) printf("Alice\n");//如果t前面的1的个数为偶数&&该序列不全为1(leap==1),Alice有主动权 else { //该序列全为1 if(n%2==0) printf("Bob\n"); else printf("Alice\n"); } } return 0; } //为什么按顺序遇到第一堆大于1的可以决定胜利?? /*举例子 1 2 1 1 2 1 1 第一堆大于1的数是 2 ,序列看成 1 2(1 1 2 1 1) 若先取得(1 1 2 1 1)的胜利,那么由于 2 那堆由Bob得到主动权,那么Bob可以先取一个 留剩一个给Alice取,那么就可使得Bob先取剩下的所有(1 1 2 1 1),由假设,Bob胜利 若先取得(1 1 2 1 1)的输,那么Bob可以把该堆全部取完,Alice先取(1 1 2 1 1)输,即Bob赢 所以,不管先取剩下的是输是赢,Bob都会赢 当序列全为1时,按正常取
|
标签:
原文地址:http://www.cnblogs.com/orchidzjl/p/4304514.html