- 原题如下:
A Funny Game
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7108 Accepted: 4464 Description
Alice and Bob decide to play a funny game. At the beginning of the game they pick n(1 <= n <= 10 6) coins in a circle, as Figure 1 shows. A move consists in removing one or two adjacent coins, leaving all other coins untouched. At least one coin must be removed. Players alternate moves with Alice starting. The player that removes the last coin wins. (The last player to move wins. If you can't move, you lose.)
Figure 1
Note: For n > 3, we use c1, c2, ..., cn to denote the coins clockwise and if Alice remove c2, then c1 and c3 are NOT adjacent! (Because there is an empty place between c1 and c3.)
Suppose that both Alice and Bob do their best in the game.
You are to write a program to determine who will finally win the game.Input
There are several test cases. Each test case has only one line, which contains a positive integer n (1 <= n <= 10 6). There are no blank lines between cases. A line with a single 0 terminates the input.Output
For each test case, if Alice win the game,output "Alice", otherwise output "Bob".Sample Input
1 2 3 0
Sample Output
Alice Alice Bob
- 题解:
- n高达1000000,考虑到还有将连续部分分裂成几段等的情况,状态数非常地多,搜索和动态规划都难以胜任。需要更加巧妙地判断胜败关系。
首先,如果能把所有的硬币分成两个完全相同的组的状态,必败态显然。不论自己采取什么选取策略,对手只要在另一组采取相同的策略,就又回到了分成两个相同的组的状态,不断这样循环下去,总会在某次轮到自己时没有硬币了。也就是说,因为对手取走了最后一枚硬币而败北。
接下来,回到正题,Alice在第一步取走了一枚或两枚硬币,原本成圈的硬币就变成了长度为n-1或n-2的链,这样只要Bob在中间位置,根据链长的奇偶性,取走一枚或两枚硬币,就可以把所有硬币正好分成了两个长度相同的链。这正如之前讨论过的,是必败态,也就是说Alice必败,Bob必胜。只不过,当n≤2时,Alice可以在第一步取光,所以胜利的是Alice。 - 注:在这类游戏当中,作出对称的状态后再完全模仿对手的策略常常是有效的。
- n高达1000000,考虑到还有将连续部分分裂成几段等的情况,状态数非常地多,搜索和动态规划都难以胜任。需要更加巧妙地判断胜败关系。
- 代码:
1 #include<cstdio> 2 #include<algorithm> 3 4 using namespace std; 5 6 int main() 7 { 8 int n; 9 while (scanf("%d", &n)) 10 { 11 if (n==0) break; 12 if (n<=2) puts("Alice"); 13 else puts("Bob"); 14 } 15 }
转载于:https://www.cnblogs.com/Ymir-TaoMee/p/9791646.html