如果已经有三个相邻的X,则先手已经输了。

如果有两个相邻的X或者两个X相隔一个.,那么先手一定胜。

除去上面两种情况,每个X周围两个格子不能再放X了,因为放完之后,对手下一轮再放一个就输了。

最后当“禁区”布满整行,不能再放X了,那个人就输了。

每放一个X,禁区会把它所在的线段“分割”开来,这若干个片段就可以看做若干个游戏的和。

设g(x)表示x个连续格子对应的SG函数值,递推来求g(x):

g(x) = mex{ g(x-3), g(x-4), g(x-5), g(x-6) xor g(1), g(x-7) xor g(2)... }

g(0) = 0, g(1) = g(2) = g(3) = 1

枚举策略时,就是模拟下一步的状态,记录其中所有必败状态。

 #include <cstdio>
#include <cstring> const int maxn = ;
char s[maxn + ];
int g[maxn + ], ans[maxn + ];
bool vis[maxn + ]; bool winning(const char* s)
{
int n = strlen(s);
for(int i = ; i < n-; i++)//已经有三个相邻的X,先手输
if(s[i] == 'X' && s[i+] == 'X' && s[i+] == 'X') return false; bool no[n+];
memset(no, false, sizeof(no));
for(int i = ; i < n; i++) if(s[i] == 'X')
{
for(int d = -; d <= ; d++)
{
if(i+d >= && i+d < n)
{
if(d != && s[i+d] == 'X') return true;//有两个X在彼此的禁区,先手胜
no[i+d] = true;//设置禁区
}
}
} no[n] = ;
int sg = ;
for(int i = ; i < n; i++)
{
if(no[i]) continue;
int cnt = ;
while(i < n && !no[i]) { i++; cnt++; }
sg ^= g[cnt];
}
return sg != ;
} int main()
{
//freopen("in.txt", "r", stdin); g[] = ;
g[] = g[] = g[] = ;
for(int i = ; i <= maxn; i++)
{//递推求函数g
memset(vis, false, sizeof(vis));
for(int j = ; i-j >= ; j++)
{
int v = ;
v ^= g[i-j];
int x = j - ;
if(x > ) v ^= g[x];
vis[v] = true; for(int j = ; ; j++) if(!vis[j]) { g[i] = j; break; }
}
} int T;
scanf("%d", &T);
while(T--)
{
scanf("%s", s);
if(!winning(s)) { printf("LOSING\n\n"); continue; } puts("WINNING");
int n = strlen(s);
memset(ans, , sizeof(ans));
int p = ;
for(int i = ; i < n; i++) if(s[i] == '.')
{
s[i] = 'X';
if(!winning(s)) ans[p++] = i+;//后继必败状态便是先手下一步的策略
s[i] = '.';
}
for(int i = ; i < p; i++)
{
if(i) printf(" ");
printf("%d", ans[i]);
}
printf("\n");
} return ;
}

代码君

UVa 10561 (SG函数 递推) Treblecross的更多相关文章

  1. 一类SG函数递推性质的深入分析——2018ACM陕西邀请赛H题

    题目描述 定义一种有根二叉树\(T(n)\)如下: (1)\(T(1)\)是一条长度为\(p\)的链: (2)\(T(2)\)是一条长度为\(q\)的链: (3)\(T(i)\)是一棵二叉树,它的左子 ...

  2. Light OJ 1296 - Again Stone Game (博弈sg函数递推)

    F - Again Stone Game Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu ...

  3. 【主席树维护mex】 【SG函数递推】 Problem H. Cups and Beans 2017.8.11

    Problem H. Cups and Beans 2017.8.11 原题: There are N cups numbered 0 through N − 1. For each i(1 ≤ i ...

  4. UVa 12034 - Race(递推 + 杨辉三角)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  5. POJ_3090 Visible Lattice Points 【欧拉函数 + 递推】

    一.题目 A lattice point (x, y) in the first quadrant (x and y are integers greater than or equal to 0), ...

  6. uva 10561 sg定理

    Problem C Treblecross Input: Standard Input Output: Standard Output Time Limit: 4 Seconds Treblecros ...

  7. UVA 10288 - Coupons(概率递推)

    UVA 10288 - Coupons option=com_onlinejudge&Itemid=8&page=show_problem&category=482&p ...

  8. uva 11375 Matches (递推)

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  9. UVA 557 - Burger(概率 递推)

     Burger  When Mr. and Mrs. Clinton's twin sons Ben and Bill had their tenth birthday, the party was ...

随机推荐

  1. 实现 iframe 子页面调用父页面中的js方法

    父页面:index.html(使用iframe包含子页面child.html) [xhtml] view plaincopyprint? <html> <head> <s ...

  2. JDBC 学习笔记(一)—— 基础知识 + 分页技术

    本文目录:  1.JDBC简介       2.使用JDBC的步骤——第一个JDBC程序       3.DriverManager ——加载数据库驱动       4.数据库URL ——标识数据库的 ...

  3. web服务器和应用服务器

    通俗的讲,Web服务器传送(serves)页面使浏览器可以浏览,然而应用程序服务器提供的是客户端应用程序可以调用(call)的方法(methods).确切一点,你可以说:Web服务器专门处理HTTP请 ...

  4. java 使用 comet4j 主动向客户端推送信息 简单例子

    [背景] 今天,一个前端的师弟问我怎样做实时聊天窗口,我毫不犹豫地说:在前台定时访问服务端呀!师弟默默地百度了一番,最后告诉我,有一种技术是后服务端动推送信息给客户端的,这种技术的名字叫comet,我 ...

  5. [转载]C#中字典集合的两种遍历

    Dictionary<string, string> dictionary = new Dictionary<string,string>(); foreach (string ...

  6. setblendstate & setdepthstencilstate

    http://msdn.microsoft.com/en-us/library/windows/desktop/ff476462(v=vs.85).aspx blendstate blendfacto ...

  7. int a[5]={1,2,3,4,5}; int *p=(int*)(&a+1); printf("%d",*(p-1)); 答案为什么是5?

    这个问题的关键是理解 &a a是一个数组名,也就是数组的首地址.对a进行取地址运算符,得到的是一个指向数组的指针!!!!这句话尤为重要!也就相当于int (*p) [5] = &a;p ...

  8. 微软发布WP SDK8.0 新增语音、应用内支付等原生API

    http://www.csdn.net/article/2012-10-31/2811338-windows-phone-8-sdk 京时间10月30日,微软在旧金山举行新一代手机操作系统Window ...

  9. PKU 1458 Common Subsequence(最长公共子序列,dp,简单)

    题目 同:ZJU 1733,HDU 1159 #include <stdio.h> #include <string.h> #include <algorithm> ...

  10. Xamarin for Visual Studio 3.11.658 Alpha 版 破解补丁

    注意:此版本为 Alpha 版,版本迭代较频繁,仅供尝鲜 前提概要 全新安装请参考 安装 Xamarin for Visual Studio. 最新稳定版请参考 Xamarin for Visual ...