码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 2999 sg函数(简单博弈)

时间:2014-04-29 17:23:45      阅读:444      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   java   color   

Stone Game, Why are you always there?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 393    Accepted Submission(s): 132


Problem Description
“Alice and Bob are playing stone game...”
“Err.... Feel bored about the stone game? Don’t be so, because stone game changes all the time!”
“What the hell are they thinking for?”
“You know, whenever Alice is trying to make fun of Bob, she asked him to play stone game with him.”
“Poor Bob... What’s the rule today?”
“It seems Alice only allows some fixed numbers of continuous stones can be taken each time. And they begin with one string of stones.”
“A string? Formed as a circle or a line?”
“A line.”
“Well, I think I may help Bob with that.”
“How?”
“I may tell him to skip this round if he has no chance to win.”
“Good idea maybe, I mean, Alice always let Bob play first, because she think herself is smart enough to beat Bob no matter how.”
“Yes, she’s actually right about herself. Let me see if Bob has a chance to win...”
......
 

 

Input
There are multiple test cases, for each test case:
The first line has a positive integer N (1<=N<=100).
The second line contains N positive integers, a1, a2 ... an, separated by spaces, which indicate the fixed numbers Alice gives.
The third line, a positive integer M. (M<=1000)
Following M lines, one positive integer K (K<=1000) each line. K means in this round, the length of the stone string.
 

 

Output
For each K, output “1” if Bob has a chance to win, output “2” if Bob has no chance, or “0” if it’s undeterminable.
 

 

Sample Input
3
1 5 1
1
1
 

 

Sample Output
1
 
题目大意:给一个正整数集合,给一串连续在石头,每次只能取集合中元素个连续的石头,每次都是最优操作,判断是先手必胜还是后手必胜。
 
 
mamicode.com,码迷
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 
 7 const int maxn=1010;
 8 int sg[maxn],Set[110],num;
 9 
10 int mex(int n)
11 {
12     if(sg[n]!=-1) return sg[n];
13     bool flag[maxn];
14     int i,j;
15     memset(flag,false,sizeof(flag));
16     for(i=0;i<num && n>=Set[i];i++)
17     {
18         for(j=1;j<=n-Set[i]+1;j++)
19         {
20             sg[j-1]=mex(j-1);
21             sg[n-j-Set[i]+1]=mex(n-j-Set[i]+1);
22             flag[sg[j-1]^sg[n-j-Set[i]+1]]=true;
23         }
24     }
25     for(i=0;i<maxn;i++) 
26         if(!flag[i]) 
27             return sg[n]=i;
28 }
29 
30 int main()
31 {
32     int n,m,k,i;
33     while(~scanf("%d",&n))
34     {
35         memset(sg,-1,sizeof(sg));
36         sg[0]=0;
37         for(i=0;i<n;i++) scanf("%d",Set+i);
38         sort(Set,Set+n);num=1;
39         for(i=1;i<n;i++) if(Set[i]!=Set[num-1]) Set[num++]=Set[i];//去重
40         scanf("%d",&m);
41         while(m--)
42         {
43             scanf("%d",&k);
44             if(sg[k]==-1) sg[k]=mex(k);
45             if(sg[k]) puts("1");
46             else puts("2");
47         }
48     }
49     return 0;
50 }
mamicode.com,码迷

 

 

hdu 2999 sg函数(简单博弈),码迷,mamicode.com

hdu 2999 sg函数(简单博弈)

标签:des   style   blog   http   java   color   

原文地址:http://www.cnblogs.com/xiong-/p/3699799.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!