POJ 1740:A New Stone Game

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;
}
时间: 2024-10-12 20:34:32

POJ 1740:A New Stone Game的相关文章

POJ 1738:An old Stone Game 石子归并 (GarsiaWachs算法)

There is an old stone game.At the beginning of the game the player picks n(1<=n<=50000) piles of stones in a line. The goal is to merge the stones in one pile observing the following rules: At each step of the game,the player can merge two adjoining

POJ 1740 A New Stone Game(博弈)题解

题意:有n个石子堆,每一个都可以轮流做如下操作:选一个石堆,移除至少1个石子,然后可以把这堆石子随便拿几次,随便放到任意的其他石子数不为0的石子堆,也可以不拿.不能操作败. 思路:我们先来证明,如果某个石子数有偶数堆,则先手必败,因为无论先手怎么做,后手都能模仿先手,最后把石子取光.显然全是偶数堆是必败态.如果有奇数堆怎么办?我们就把最大的奇数堆取光,然后把其他奇数堆变成偶数堆.但是一定能保证可以吗?答案是可以.假设奇数堆的石子数为 x1,x2,x3...xn,那么我们分别给每一堆加上x2-x1

poj 1740 A New Stone Game nim变形

题意: 给n堆石子,两人交替,选择一堆石头后先拿去任意颗,再把剩下的放到其他任意堆,最先拿完所有石子赢,问先手必胜还是必败. 分析: 解决此类问题的一种的思路是先构造策略,然后判断此策略能否满足1.必胜态可到必败态.2.必败态无法到必败态. 代码: //poj 1740 //sep9 #include <iostream> #include <algorithm> using namespace std; const int maxN=128; int a[maxN]; int m

POJ 1963:All in All

All in All Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 27707   Accepted: 11381 Description You have devised a new encryption technique which encodes a message by inserting between its characters randomly generated strings in a clever

POJ 1679:The Unique MST(次小生成树&amp;&amp;Kruskal)

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19941   Accepted: 6999 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undire

POJ 1659:Frogs&#39; Neighborhood(Havel-Hakimi定理)

Frogs' Neighborhood Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 6898   Accepted: 3006   Special Judge Description 未名湖附近共有N个大小湖泊L1, L2, ..., Ln(其中包括未名湖),每个湖泊Li里住着一只青蛙Fi(1 ≤ i ≤ N).如果湖泊Li和Lj之间有水路相连,则青蛙Fi和Fj互称为邻居.现在已知每只青蛙的邻居数目x1, x2, ..

POJ 1422:Air Raid(最大独立集)

Air Raid Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6547   Accepted: 3896 Description Consider a town where all the streets are one-way and each street leads from one intersection to another. It is also known that starting from an i

POJ 2965:The Pilots Brothers&#39; refrigerator

The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18080   Accepted: 6855   Special Judge Description The game "The Pilots Brothers: following the stripy elephant" has a quest where a player needs to o

POJ 1904:King&#39;s Quest【tarjan】

题目大意:给出一个二分图的完美匹配(王子和公主的烧死名单表),二分图x部和y部均只有n个点,问对于每一个x部的点,他能选择哪些点与之匹配 使得与之匹配后,剩余图的最大匹配仍然是n 思路:这题是大白书379页二分图的压轴题,在图论刷的题还不多时思考过这题,现在想来也不难想 这题引人瞩目的一点便是预先给出了一个二分图的初始匹配 对每个点枚举后增广显然不怎么可行,那么还是图论问题的经典思考方式,点和边各表示什么 题目的输入天然的给出了一个图,但对这题好像没什么用处,于是开始思考把给出的初始匹配的每条边