poj1740| A NEW STONE game| game theory

Source: Internet
Author: User

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 and pile that still have 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.
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

Analysis: The great God is the tectonic game orz ... In fact, look at the big God after the problem is not difficult.

Starting from the simple, first, a bunch of words to win.

If there are two piles, then only two stacks are the same when the initiator loses. If the two stacks are different, the initiator can cause two piles of the same situation so as not to lose.

If there are three piles, the initiator can take a bunch and make two identical cases.

We found that the n heap of stones, if the number of stones symmetrical, that is, each number of stones have even heap, the initiator will lose. Because the initiator can not take all the light, then the hand as long as the imitation of the act can always have a stone is desirable.

Continue to reason, we will find that only this situation will lose the initiator.

#include <iostream>#include<cstdio>#include<algorithm>using namespacestd;intn,a[ One];intMain () { while(SCANF ("%d",&N)) {if(n==0)return 0;  for(intI=1; i<=n; i++) scanf ("%d",&A[i]); if(n%2==1) printf ("1\n"); Else         {             inti; Sort (a+1, a+n+1);  for(i=1; I<=n;i + =2)                 if(a[i]!=a[i+1]) {printf ("1\n"); Break; } if(i>n) printf ("0\n"); }  }
}

poj1740| A NEW STONE game| game theory

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.