HDU 1536 poj 2960 博弈 SG函数

坑爹啊,一到简单的SG博弈题,折磨了我一个下午,竟然是在函数的递归调用中使用了全局的标志数组进行初始化。。。。

View Code
 1 /*
2 * Author:lonelycatcher
3 * Problem:HDU 1536
4 * Type:组合博弈,GS函数
5 */
6 #include <iostream>
7 #include<stdio.h>
8 #include<algorithm>
9 #include<string.h>
10 #include<cstdlib>
11 using namespace std;
12 int s[100],k,m,heapnum,heap,SG[10010];
13 //int visited[100];全局标志数组,在递归调用中,坑爹啊!!!
14 int getSG(int x)
15 {
16 if(SG[x]!=-1)return SG[x];
17 int i,temp;
18 //memset(visited,0,sizeof(visited));全局标志数组,在递归调用中,坑爹啊!!!
19 int visited[100]={0};
20 for(i=0;i<k;i++)
21 {
22 temp=x-s[i];
23 if(temp<0)break;
24 if(SG[temp]==-1)
25 {
26 SG[temp]=getSG(temp);
27 }
28 visited[SG[temp]]=1;
29 }
30 for(i=0;;i++)
31 {
32 if(visited[i]==0)break;
33 }
34 return i;
35 }
36 int main()
37 {
38 setbuf(stdout,NULL);
39 int i,ans;
40 while(scanf("%d",&k))
41 {
42 if(k==0)break;
43 for(i=0;i<k;i++)
44 {
45 scanf("%d",&s[i]);
46 }
47 sort(s,s+k);
48 scanf("%d",&m);
49 memset(SG,-1,sizeof(SG));
50 SG[0]=0;
51 while(m--)
52 {
53 ans=0;
54 scanf("%d",&heapnum);
55 for(i=0;i<heapnum;i++)
56 {
57 scanf("%d",&heap);
58 if(SG[heap]==-1)
59 {
60 SG[heap]=getSG(heap);
61 }
62 ans^=SG[heap];//避免重复运算
63 }
64 if(ans)printf("W");
65 else printf("L");
66 }
67 printf("\n");
68 }
69 return 0;
70 }

posted on 2011-08-20 17:49  lonelycatcher  阅读(314)  评论(0编辑  收藏  举报

导航