博弈論專題——推理與動態規劃相關博弈之POJ2484 POJ1740(模仿遊戲)
本專題題目均來自挑戰程式設計競賽例題和練習
A Funny Game
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5198 | Accepted: 3244 |
Description
Alice and Bob decide to play a funny game. At the beginning of the game they pick n(1 <= n <= 106) 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.
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 <= 106). 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個硬幣圍成一個圈,Alice和Bob輪流取出一枚硬幣,或者兩枚連續的硬幣,硬幣取走後會留下空位,取走最後一枚硬幣的人獲勝。
首先,如果只有一枚或者兩枚硬幣,Alice一定可以全部取走。
如果是三枚或者三枚以上呢?
首先考慮一個問題,如果硬幣可以恰好分為狀態相同的兩組,後手一定可以模仿先手的行為,進而獲得勝利。說明這是必敗態。
當第一次Alice取走一枚或兩枚硬幣,Bob一定可以把硬幣分為相同的兩組。那麼Bob必勝。所以就是n大於2則bob勝。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
using namespace std;
const int MAXN =15;
int n;
int a[MAXN];
int main(){
while(scanf("%d",&n)&&n){
for(int i=0;i<n;i++)
scanf("%d",a+i);
sort(a,a+n);
int win=0;
if(n%2==1)
win=1;
else{
for(int i=1;i<n;i+=2){
if(a[i]!=a[i-1]){
win=1;
break;
}
}
}
printf("%d\n",win);
}
}
A New Stone Game
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 5671 | Accepted: 3102 |
Description
Alice and Bob decide to play a new stone game.At the beginning of the game they pick n(1<=n<=10) piles of stones in a line. Alice and Bob move the stones in turn.
At each step of the game,the player choose a pile,remove at least one stones,then freely move stones from this pile to any other pile that still has stones.
For example:n=4 and the piles have (3,1,4,2) stones.If the player chose the first pile and remove one.Then it can reach the follow states.
2 1 4 2
1 2 4 2(move one stone to Pile 2)
1 1 5 2(move one stone to Pile 3)
1 1 4 3(move one stone to Pile 4)
0 2 5 2(move one stone to Pile 2 and another one to Pile 3)
0 2 4 3(move one stone to Pile 2 and another one to Pile 4)
0 1 5 3(move one stone to Pile 3 and another one to Pile 4)
0 3 4 2(move two stones to Pile 2)
0 1 6 2(move two stones to Pile 3)
0 1 4 4(move two stones to Pile 4)
Alice always moves first. 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.
At each step of the game,the player choose a pile,remove at least one stones,then freely move stones from this pile to any other pile that still has stones.
For example:n=4 and the piles have (3,1,4,2) stones.If the player chose the first pile and remove one.Then it can reach the follow states.
2 1 4 2
1 2 4 2(move one stone to Pile 2)
1 1 5 2(move one stone to Pile 3)
1 1 4 3(move one stone to Pile 4)
0 2 5 2(move one stone to Pile 2 and another one to Pile 3)
0 2 4 3(move one stone to Pile 2 and another one to Pile 4)
0 1 5 3(move one stone to Pile 3 and another one to Pile 4)
0 3 4 2(move two stones to Pile 2)
0 1 6 2(move two stones to Pile 3)
0 1 4 4(move two stones to Pile 4)
Alice always moves first. 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
The input contains several test cases. The first line of each test case contains an integer number n, denoting the number of piles. The following n integers describe the number of stones in each pile at the beginning of the game, you may assume the number of
stones in each pile will not exceed 100.
The last test case is followed by one zero.
The last test case is followed by one zero.
Output
For each test case, if Alice win the game,output 1,otherwise output 0.
Sample Input
3 2 1 3 2 1 1 0
Sample Output
1 0
這應該算是難度比較大的推理性質的博弈。每一輪你可以減少任意一堆石子至少一個,可以再從剩下的裡面分出一些給其他堆。
也是一個模仿遊戲類的題目。
如果石子可以完全分為對稱的兩部分,那麼後手一定通過模仿先手的行為獲得勝利。
如果一開始石子對稱,那麼後手勝。
如果不呢?
那麼先手一定可以把石子分為對稱的兩部分。
為何?
不論石子的堆數是奇數堆還是偶數堆,
都可以通過改變最高的那堆使得石子對稱。
把石子按數量排序,相鄰的高度差的和一定小於最高的石子。也就是最高的石子可以彌補相鄰石子的高度差。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
using namespace std;
const int MAXN =15;
int n;
int a[MAXN];
int main(){
while(scanf("%d",&n)&&n){
for(int i=0;i<n;i++)
scanf("%d",a+i);
sort(a,a+n);
int win=0;
if(n%2==1)
win=1;
else{
for(int i=1;i<n;i+=2){
if(a[i]!=a[i-1]){
win=1;
break;
}
}
}
printf("%d\n",win);
}
}
相關文章
- 博弈論專題——推理與動態規劃相關博弈之POJ2348動態規劃
- 博弈論入門之nim遊戲遊戲
- 博弈論專練
- 博弈論入門之威佐夫博弈
- 組合遊戲與博弈論基礎遊戲
- 博弈論入門之斐波那契博弈
- 遊戲博弈論研究:最優解策略與混合迴圈戰略博弈遊戲
- 博弈論
- 博弈論進階之Anti-SG遊戲與SJ定理遊戲
- 博弈論題目選做
- 當博弈論遇上機器學習:一文讀懂相關理論機器學習
- 博弈論基礎之sg函式與nim函式
- 動態規劃-揹包01問題推理與實踐動態規劃
- 動態規劃專題動態規劃
- 博弈論詳解
- 博弈論小記
- 博弈論總結
- 博弈論入門
- 博弈論入門之巴什博奕
- 博弈論進階之SG函式函式
- 博弈論進階之Multi-SG
- 博弈論進階之Every-SG
- 來自一篇推理小說的博弈問題
- 關於解決博弈論問題的SG函式函式
- 博弈論進階之樹的刪邊遊戲與無向圖的刪邊遊戲遊戲
- 博弈論(長期更新)
- 淺談動態規劃以及相關的股票問題動態規劃
- A Multiplication Game (博弈,規律)GAM
- 【演算法學習筆記】博弈論淺析之遊戲類演算法筆記遊戲
- 動態規劃專題之---- Unique Binary Search Trees動態規劃
- 動態規劃專題之----198. House Robber動態規劃
- 動態規劃專題之----213. House Robber II動態規劃
- 動態規劃方法論動態規劃
- 有關動態規劃的相關優化思想動態規劃優化
- 博弈論之軟體測試的價值
- 【博弈論】組合遊戲及SG函式淺析遊戲函式
- 動態規劃之理論分析動態規劃
- 如何構建多人遊戲生態環境——用博弈論強化體驗遊戲