hdu 1536 S-Nim
2017-08-01 22:12
309 查看
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
【题意】
以我仅剩不多的英文水平,硬是没懂这题说啥,没办法只有搜别人的博客,总算是懂了。用我的话解释一下。
用取石子来说明:
现在你和对手约定玩取石子的游戏,但是你们不想游戏太简单,所以每次取的时候只能取特定的值,这些值放在一个集合里面,然后就是一局局的游戏,对于每一局游戏你要输出一个值表示先手是否能必胜。
还是用测试用例说话:
首先是多组输入,对于每一组输入,首先是能选择的取子数量集合的长度,然后就是能取的各个数量。然后一行是按照当前集合的游戏局数,对于每一局游戏有一行 ,第一个数据表示石子的堆数,然后就是每一堆的数量多少。
【分析】
只能说题意远比题目难,虽然是第一次做SG的题目,但是过程还是好理解的。然后就是getSG那个函数,应该就是模板了。关键有一点是能选择的石子数量得排个序,开始没注意,wa了一发(主要是测试用例给的太好,没注意)。然后就没啥了。如果不懂的话就可以去好好学学SG(主要是我比较水,讲不清楚,有时间再来更新)。
【代码】
#include <cstdio> #include <cstdlib> #include <cstring> #include <map> #include <list> #include <algorithm> #include <iostream> using namespace std; int len; int canchoose[105]; int num; int SG[10010]; int getSG(int x) { if(SG[x]!=-1) return SG[x]; bool flag[105]; memset(flag,false,sizeof(flag)); for(int i=0;i<len;i++) { if(canchoose[i]>x) break; int tmp=x-canchoose[i]; if(SG[tmp]==-1) SG[tmp]=getSG(tmp); flag[SG[tmp]]=true; } for(int i=0;;i++) if(!flag[i]) return (SG[x]=i); } int main() { // freopen("in.txt","r",stdin); while(cin>>len&&len) { for(int i=0;i<len;i++) cin>>canchoose[i]; sort(canchoose,canchoose+len); int T; memset(SG,-1,sizeof(SG)); cin>>T; while(T--) { int dui; cin>>dui; int ans=0; for(int i=0;i<dui;i++) { cin>>num; ans^=getSG(num); } cout<<(ans?'W':'L'); } cout<<endl; } return 0; }
相关文章推荐
- hdu 1536 S-Nim(SG函数)
- HDU 1536 S-Nim
- hdu 1536/ hdu 1944 S-Nim(sg函数)
- hdu 1536 S-Nim
- hdu 1536 S-Nim
- hdu 1536 S-Nim(SG函数)
- hdu 1536 S-Nim(SG函数)
- HDU 1536 S-Nim
- HDU 1536 S-Nim [SG函数]【博弈】
- HDU - 1536 S-Nim
- HDU 1536 S-Nim (博弈 sg函数 Nim和)
- HDU 1536 S-Nim
- HDU 1536 与 1944 S-Nim
- hdu1536 S-Nim
- hdu 1536 S-Nim(SG函数)
- hdu_1536_S-Nim(DFS_SG博弈)
- HDU 1536 S-Nim(SG博弈)
- HDU 1536 && HDU 1944 S-Nim (Nim博弈、SG函数模板)
- 博弈论 ( Nim游戏+SG模板)——S-Nim ( HDU 1536 )
- hdu 1536 &&hdu1944 S-Nim (sg函数打表)