博弈論專題——推理與動態規劃相關博弈之POJ2484 POJ1740(模仿遊戲)

say_c_box發表於2016-08-12

本專題題目均來自挑戰程式設計競賽例題和練習

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.

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. 

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. 

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);
    }
}







相關文章