POJ2484 A Funny Game(博弈&對稱狀態)

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


《挑戰程序設計競賽》P307

題意:n枚硬幣排成一個圈。Alice和Bob輪流從中取一枚或兩枚硬幣。不過,取兩枚時,所取的兩枚硬幣必須是連續的。硬幣取走之後留下空位,相隔空位的硬幣視爲不連續的。Alice開始先取,取走最後一枚硬幣的一方獲勝。當雙方都採取最優策略時,誰會獲勝?


解題思路:

n高達1000000,考慮到還有將連續部分分裂成幾段等的情況,狀態數非常地多,搜索和動態規劃法難以勝任。需要更加巧妙地判斷勝敗關係。

首先,試想一下如下情況。能夠把所有的硬幣分成像下圖這樣的兩個完全相同的組的狀態,是必勝狀態?還是必敗狀態?(沒有圖 圖詳見P307 0.0)

事實上這是必敗態。不論自己採取什麼選取策略,對手只要在另一組採取相同的策略,就又回到了分成兩個相同的組的狀態。

必定能夠再次回到同樣的情況:不論自己取走哪些硬幣-->如果對手也取走對應的硬幣-->則回到同樣的情況

不斷這樣循環下去,總會在某次輪到自己時沒有硬幣了。也就是說,因爲對手取走了最後一枚硬幣而敗北。

接下來,讓我們回到正題。Alice在第一步取走了一枚或兩枚硬幣之後,原本成圈的硬幣就變成了長度爲n-1或是n-2的鏈。這樣只要Bobby在中間位置,根據鏈長的奇偶性,取走一枚或兩枚硬幣,就可以把所有硬幣正好分成了兩個長度相同的鏈。


這正如我們前面所討論的一樣,是必敗態。也就是說,Alice必敗,Bob必勝。只不過,當n<=2時,Alice可以在第一步取光,所以勝利的是Alice。在這類遊戲中,作出對稱的狀態後再完全模仿對手的策略常常是有效的。


AC代碼:

#include<stdio.h>

int main()
{
	int n;
	while(~scanf("%d",&n)&&n)
	{
		if(n<=2) printf("Alice\n");
		else printf("Bob\n");
	}
	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章