POJ 1740分析过程

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. 

分析:(first表示先手胜,last表示后手胜)

一堆的情况->first

下面分析两堆的情况:

1,1->last

1,2->first(因为此状态可以转化为last的状态)

1,n->first

2,2->last(无法转化为last的状态)

2,n->first(可以转化为2,2的状态)

3,3->last

猜想:(n,m)当且仅当n==m时后手胜。

分析玩两堆之后便可以把两堆的情况推广到n堆的情况:

对于三堆 a1<=a2<=a3的情况:

先手肯定可以制造出 剩下两堆且n==m的情况,让后手陷于必败的局势。

先手可以a3中取走a3+a1-a2个,然后将剩下的a2-a1个移动到a1堆中,制造出a1==a2的局势,根据我们之前的分析,面对此种情况必败。

接下来分析四堆的情况:

a1<=a2<=a3<=a4.

如果a1==a2&&a3==a4,那么先手必败,因为后手总可以保持这样的局势,总令先手面对的是相等局势,最后先手必败。假若先手从a3中取走k个,并将j个移动到a1,那么后手可以从a4中取走k个,将j个移动到a2堆中。

其他情况先手必胜。

 

结论:面对n堆,a1,a2,,,,,an;如果a1==a2,a3==a4,,,an-1==an,那么后手胜,否则先手胜。

#include"iostream" 
#include"algorithm" 
using namespace std; 
int main() 
{ 
    int n; 
    while(cin>>n,n) 
    { 
        int a[11],i,k=1; 
        for(i=0;i<n;i++) 
            cin>>a[i]; 
        sort(a,a+i); 
        if(n%2==0) 
        { 
            for(i=0;i<n-1;i+=2) 
                if(a[i]!=a[i+1]) { k=0;break;} 
        } 
        if(k&&n%2==0) 
            cout<<0<<endl; 
        else cout<<1<<endl; 
    } 
    return 0; 
}


posted @ 2011-05-10 12:18  Ac_smile  阅读(231)  评论(0编辑  收藏  举报