hdu 1536 sg (dfs实现)
S-Nim
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5637 Accepted Submission(s): 2414
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.
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
1 #include<cstdio> 2 #include<algorithm> 3 using namespace std; 4 #define N 100+10 5 int knum,mnum,lnum; 6 int ans[N],si[N],hi[N],sg[10010]; 7 int mex(int x)//求x的sg值(可作为模版应用) 8 { 9 if(sg[x]!=-1) return sg[x]; 10 bool vis[N]; 11 memset(vis,false,sizeof(vis)); 12 for(int i=0;i<knum;i++) { 13 int temp=x-si[i]; 14 if(temp<0) break; 15 sg[temp]=mex(temp); 16 vis[sg[temp]]=true; 17 } 18 for(int i=0;;i++) { 19 if(!vis[i]) { 20 sg[x]=i; break; 21 } 22 } 23 return sg[x]; 24 } 25 int main() { 26 while(scanf("%d",&knum) && knum) { 27 for(int i=0;i<knum;i++) 28 scanf("%d",&si[i]); 29 sort(si,si+knum); 30 memset(sg,-1,sizeof(sg)); 31 sg[0]=0; 32 memset(ans,0,sizeof(ans)); 33 scanf("%d",&mnum); 34 for(int i=0;i<mnum;i++) { 35 scanf("%d",&lnum); 36 for(int j=0;j<lnum;j++) { 37 scanf("%d",&hi[i]); ans[i]^=mex(hi[i]);//尼姆博弈 38 } 39 } 40 for(int i=0;i<mnum;i++) 41 { 42 if(ans[i]==0) printf("L"); 43 else printf("W"); 44 } 45 printf("\n"); 46 } 47 return 0; 48 }
1 #include"iostream" 2 #include"algorithm" 3 #include"string.h" 4 using namespace std; 5 int s[101],sg[10001],k; 6 int getsg(int m) 7 { 8 int hash[101]={0}; 9 int i; 10 for(i=0;i<k;i++){ 11 if(m-s[i]<0) 12 break; 13 if(sg[m-s[i]]==-1) 14 sg[m-s[i]]=getsg(m-s[i]); 15 hash[sg[m-s[i]]]=1; 16 } 17 for(i=0;;i++) 18 if(hash[i]==0) 19 return i; 20 21 22 } 23 int main() 24 { 25 //int k; 26 // freopen("game.in","r",stdin); 27 //freopen("game.out","w",stdout); 28 while(cin>>k,k) 29 { 30 int i; 31 for(i=0;i<k;i++) 32 cin>>s[i]; 33 sort(s,s+k); 34 memset(sg,-1,sizeof(sg)); 35 sg[0]=0; 36 int t; 37 cin>>t; 38 while(t--) 39 { 40 41 int n,m; 42 cin>>n; 43 int ans=0; 44 while(n--) 45 { 46 cin>>m; 47 if(sg[m]==-1) 48 sg[m]=getsg(m); 49 ans^=sg[m]; 50 } 51 if(ans) 52 cout<<'W'; 53 else cout<<'L'; 54 } 55 cout<<endl; 56 } 57 return 0; 58 }