1. 程式人生 > >POJ 1740:A New Stone Game

POJ 1740:A New Stone Game

always you 簡單 sum type down .text i++ class

A New Stone Game

Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 5113 Accepted: 2806

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

題意:Alice和Bob沒事就喜歡玩遊戲,玩的還竟都是本身特別無聊但一出題就特別高端的那種拿石子遊戲(……)。給定幾個堆的石子,A和B分別拿一個堆中隨意數量的石子。然後可放可不放到其它有石子的堆中,誰最後沒有石子拿誰就輸了。每次都是A先拿,輸出1代表A贏,輸出0代表B贏

發現如今做過的題,包含博弈論還有非常多類型的題目。都是從最小的情況最簡單的情況開始想,之後在往後面去推。當然dp這樣的就更不用說了。如果就有一個堆,那肯定是A贏,由於A直接把全部的石子拿走,B就沒有石子拿了。如果有兩個堆,如果是兩個相等的堆。那就是B贏。由於A不管怎麽拿,B都能夠在還有一個堆上做同樣的動作,導致一定是B拿到了最後的石子。

如果是兩個不同數量的堆。那又一定是A贏。由於A先拿。能夠導致上面相等的堆的情況,而這時A、B的拿的順序已經換過來了,所以A贏。所以這時我們會發現拿走石子能夠,放回石子到其它有石子的堆裏這個動作是沒實用的。由於A、B都採取最好策略,每一堆石子數量比不放回的情況多是沒實用的,最後也都是拿走的命。

所以就這麽一直地推下去。會發現整個過程就是看石子堆是不是一對一對的過程,奇數堆的一定是A贏,偶數堆的要是一對一對就是B贏。否則就是A贏。這樣代碼就好寫了

所以這次做題的經驗就是以後拿到博弈的題目,都要從最小最簡單的情況搞起,再往後面推是一個好方法。

代碼:

#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;

int n[105];

int main()
{
    int num;
    while(cin>>num)
    {
        if(!num)
            break;
        memset(n,0,sizeof(n));

        int i,temp;

        if(num%2)
        {
            for(i=1;i<=num;i++)
                cin>>temp;
            cout<<1<<endl;
        }
        else
        {
            for(i=1;i<=num;i++)
            {
                cin>>temp;
                n[temp]++;
            }
            int flag=0;
            for(i=0;i<=104;i++)
            {
                if(n[i]%2)
                    flag=1;
            }
            if(flag)
                cout<<1<<endl;
            else
                cout<<0<<endl;
        }
    }
    return 0;
}

POJ 1740:A New Stone Game