HDU 1536 S-Nim(SG经典博弈)

来源:互联网 发布:仿麦客网表单源码 编辑:程序博客网 时间:2024/06/05 03:00

题目链接

题意:给你可以进行操作的步数,然后分别给你一堆石子,求解每一次给出的石子先者输赢的结果,经典的SG博弈,SG具体不解释

#include <cstdio>#include <cstring>#include <algorithm>#define FIN freopen("input.txt", "r", stdin)using namespace std;const int MAXN = 1e4 + 10;const int MAXM = 1e2 + 5;int s[MAXM];int SG[MAXN], k;intgetsg(int m) {//使用记忆化搜索进行处理,用于处理超时问题int Hash[MAXM] = {0};inti;for(i = 0; i < k; i ++) {if(m - s[i] < 0) {break;}if(SG[m - s[i]] == -1) {SG[m - s[i]] = getsg(m - s[i]);}Hash[SG[m - s[i]]] = 1;}for(i = 0; ; i ++) {if(Hash[i] == 0) {return i;}}}int main() {//FIN;while(~scanf("%d", &k), k) {for(int i = 0; i < k; i ++) {scanf("%d", &s[i]);}sort(s, s + k);int m, n;scanf("%d", &m);memset(SG, -1, sizeof(SG));while(m --) {int sum = 0, x;scanf("%d", &n);for(int i = 0; i < n; i ++) {scanf("%d", &x);if(SG[x] == -1) SG[x] = getsg(x);sum ^= SG[x];}printf(sum ? "W" : "L");}printf("\n");}return 0;}


1 0
原创粉丝点击