POJ 1740:a New Stone Game

Source: Internet
Author: User

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 on 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 this still has stones.
For example:n=4 and the piles has (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 stones to Pile 2)
0 1 6 2 (move stones to Pile 3)
0 1 4 4 (move stones to Pile 4)
Alice always moves first. Suppose that both Alice and Bob does their best in the game.
You is to write a program to determine who'll finally win the game.

Input
The input contains several test cases. The first line of all 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 Numbe R of stones in each pile would not exceed 100.
The last test case was 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

Test instructions: Alice and Bob are free to play games and play with their own special boring but a special high-end type of the kind of stone games (...). Given a few heaps of stones, A and B each take a heap of any number of stones, and then can be placed in other gravel heap, who finally did not have the stone to lose. Each time is a first take, the output 1 for a win, the output of 0 for B win found now do the problem, including game theory and many types of topics, are from the smallest case of the simplest case to start thinking, after the push in the back, of course, DP this is not to mention. Suppose there is a heap, that must be a win, because a directly take all the stones away, b there is no stone to take. Suppose there are two heaps, and if it is two equal heaps, that is B win. Because a does whatever it takes, B can do the same thing on the other heap, causing the B to get the last stone. If it is two different quantities of heap, it must be a win, because a first take, can lead to the above equal heap situation, and then A, B took the order has been replaced, so a win. So then we will find that take the stone can, put back stones to other gravel heap this action is useless, because a, b all take the best strategy, each heap of stones than not put back the situation is no use, and finally is taken away life. So always push down, will find the whole process is to see the gravel heap is not a pair of process, the odd heap must be a win, even if the heap is a one-to-one pair is B win, otherwise is a win. So the code is good to write so the experience of this problem is to get the game after the topic, all from the smallest and simplest situation, and then push back is a good way. Code:
#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;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 1740:a New Stone Game

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.