HDU 1536

S-Nim
Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1782    Accepted Submission(s): 802
Problem Description
Arthur and his sister Caroll have been playing a game called Nim for some time now. Nim is played as follows:
  The starting position has a number of heaps, all containing some, not necessarily equal, number of beads.
  The players take turns chosing a heap and removing a positive number of beads from it.
  The first player not able to make a move, loses.
Arthur and Caroll really enjoyed playing this simple game until they recently learned an easy way to always be able to find the best move:
  Xor the number of beads in the heaps in the current position (i.e. if we have 2, 4 and 7 the xor-sum will be 1 as 2 xor 4 xor 7 = 1).
  If the xor-sum is 0, too bad, you will lose.
  Otherwise, move such that the xor-sum becomes 0. This is always possible.
It is quite easy to convince oneself that this works. Consider these facts:
  The player that takes the last bead wins.
  After the winning player's last move the xor-sum will be 0.
  The xor-sum will change after every move.
Which means that if you make sure that the xor-sum always is 0 when you have made your move, your opponent will never be able to win, and, thus, you will win. 
Understandibly it is no fun to play a game when both players know how to play perfectly (ignorance is bliss). Fourtunately, Arthur and Caroll soon came up with a similar game, S-Nim, that seemed to solve this problem. Each player is now only allowed to remove a number of beads in some predefined set S, e.g. if we have S =(2, 5) each player is only allowed to remove 2 or 5 beads. Now it is not always possible to make the xor-sum 0 and, thus, the strategy above is useless. Or is it? 
your job is to write a program that determines if a position of S-Nim is a losing or a winning position. A position is a winning position if there is at least one move to a losing position. A position is a losing position if there are no moves to a losing position. This means, as expected, that a position with no legal moves is a losing position.
 
Input
Input consists of a number of test cases. For each test case: The first line contains a number k (0 < k ≤ 100 describing the size of S, followed by k numbers si (0 < si ≤ 10000) describing S. The second line contains a number m (0 < m ≤ 100) describing the number of positions to evaluate. The next m lines each contain a number l (0 < l ≤ 100) describing the number of heaps and l numbers hi (0 ≤ hi ≤ 10000) describing the number of beads in the heaps. The last test case is followed by a 0 on a line of its own.
 
Output
For each position: If the described position is a winning position print a 'W'.If the described position is a losing position print an 'L'. Print a newline after each test case.
 
Sample Input
2 2 5
3
2 5 12
3 2 4 7
4 2 3 7 12
5 1 2 3 4 5
3
2 5 12
3 2 4 7
4 2 3 7 12
0
 
Sample Output
LWW
WWL
//很郁闷,因为题目看不懂,只能看别人的代码。才知道意思……
//题意:有一个集合有S个数。玩N次取石子游戏,每次只能取S集合里的数个石子。求判先手胜负。

 1 //递归写的
 2 #include<iostream>
 3 using namespace std;
 4 
 5 int t;
 6 int num[101];
 7 int sg[10001];
 8 
 9 int Get_sg(int n)
10 {
11     if(n==0) return 0;
12     int i;
13     bool mex[101]={0};
14     for(i=0;i<t;i++)
15     {
16         if(n>=num[i])
17         {
18             int temp=n-num[i];
19             if(sg[temp]==-1)
20             {
21                 sg[temp]=Get_sg(temp);
22             }
23             mex[sg[temp]]=true;
24         }
25     }
26     for(i=0;;i++)
27     {
28         if(!mex[i])
29         {
30             return i;
31         }
32     }
33 }
34 
35 int main()
36 {
37     int i,l,m,k;
38     while(scanf("%d",&t)!=EOF,t)
39     {
40         for(i=0;i<t;i++)
41         {
42             scanf("%d",&num[i]);
43         }
44         scanf("%d",&l);
45         memset(sg,-1,sizeof(sg));
46         while(l--)
47         {
48             int temp=0;
49             scanf("%d",&k);
50             while(k--)
51             {
52                 scanf("%d",&m);
53                 if(sg[m]==-1)
54                 {
55                     sg[m]=Get_sg(m);
56                 }
57                 temp^=sg[m];
58             }
59              if(temp)
60             {
61                 printf("W");
62             }
63             else
64             {
65                 printf("L");
66             }
67         }
68         putchar('\n');
69     }
70     return 0;
71 }
72 

posted on 2012-03-17 14:46 煙雨默嘫 阅读(411) 评论(0)  编辑 收藏 引用 所属分类: HDU


只有注册用户登录后才能发表评论。
相关文章:
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2012年3月>
26272829123
45678910
11121314151617
18192021222324
25262728293031
1234567

导航

统计

公告

  • 默默的大神
  • 常用链接

    留言簿

    随笔分类

    随笔档案

    文章档案

    相册

    最新随笔

    搜索

    积分与排名

    最新评论

    阅读排行榜

    评论排行榜