HDU1536&&POJ2960 S-Nim(SG函数博弈)
Posted Ritchie丶
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU1536&&POJ2960 S-Nim(SG函数博弈)相关的知识,希望对你有一定的参考价值。
Time Limit: 2000MS | Memory Limit: 65536KB | 64bit IO Format: %I64d & %I64u |
Description
- 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.
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.
- 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.
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
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
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
经典的Nim游戏题目中已经给出了——每一堆选取的数量没有限制。
S-Nim游戏仅仅是限制了每一次从每一堆中选取的个数,依旧用sg函数计算即可。
经典的Nim游戏中sg(x) = x,所以结果就是每一堆的状态直接xor即可,S-Nim游戏先计算每一堆的sg函数值,然后判断方法依旧是用xor。
#include <iostream> #include <cstring> #include <algorithm> using namespace std; #define N 102 int n,m,l,s[N],sg[10005],ans,a; bool vis[10005]; void c() { sg[0]=0; for(int i=1;i<=10000;i++) { memset(vis,0,sizeof(vis)); for(int j=0;j<n&&s[j]<=i;j++) vis[sg[i-s[j]]]=1; for(int x=0;x<=10000;x++) if(!vis[x]) { sg[i]=x; break; } } } int main() { while(cin>>n&&n) { for(int i=0;i<n;i++) cin>>s[i]; sort(s,s+n); c(); cin>>m; while(m--) { ans=0; cin>>l; while(l--) { cin>>a; ans^=sg[a]; } if(ans) cout<<"W"; else cout<<"L"; } cout<<endl; } return 0; }
对sg函数的理解:
#include <iostream> #include <cstring> #include <algorithm> using namespace std; #define N 102 int n,m,l,s[N],sg[10005],ans,a; bool vis[10005]; void c() { sg[0]=0; for(int i=1;i<=10000;i++) { memset(vis,0,sizeof(vis)); for(int j=0;j<n&&s[j]<=i;j++) vis[sg[i-s[j]]]=1; //里面的sg[]是sg[i]的后继 vis[sg[]] 指的是求mex for(int x=0;x<=10000;x++) if(!vis[x]) { sg[i]=x; break; } } } int main() { while(cin>>n&&n) { for(int i=0;i<n;i++) cin>>s[i]; sort(s,s+n); c(); for(int i=0;i<30;i++) cout<<"i: "<<i<<" sg[i]: "<<sg[i]<<endl; /*cin>>m; while(m--) { ans=0; cin>>l; while(l--) { cin>>a; ans^=sg[a]; } if(ans) cout<<"W"; else cout<<"L"; } cout<<endl;*/ } return 0; }
让我们再来考虑一下顶点的SG值的意义。当g(x)=k时,表明对于任意一个0<=i<k,都存在x的一个后继y满足g(y)=i。
也就是说,当某枚棋子的SG值是k时,我们可以把它变成0、变成1、……、变成k-1,但绝对不能保持k不变。
不知道你能不能根据这个联想到Nim游戏,Nim 游戏的规则就是:每次选择一堆数量为k的石子,
可以把它变成0、变成1、……、变成k-1,但绝对不能保持k不变。这表明,如果将n枚棋子所在的顶点的 SG值看作n
堆相应数量的石子,那么这个Nim游戏的每个必胜策略都对应于原来这n枚棋子的必胜策略!
以上是关于HDU1536&&POJ2960 S-Nim(SG函数博弈)的主要内容,如果未能解决你的问题,请参考以下文章
hdu1501&&poj2192 Zipper(DFS)
poj2243 && hdu1372 Knight Moves(BFS)
扫描线三巨头 hdu1928&&hdu 1255 && hdu 1542 [POJ 1151]
POJ 3486 & HDU 1913 Computers(dp)