如果已经有三个相邻的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. 将Xml字符串转换成(DataTable || DataSet || XML)对象

    今天用到一个功能:就是把从数据库读出来的内容转换成XML字符串流格式,并输出给一个功能函数.在写的过程,为方便以后的使用,我对这一功能进行分装.该类的具体格式如下:XmlConvert类命名空间:Ni ...

  2. beego 0.9.0 中智能路由AutoRouter的使用方法及源码解读

    了解beego的开发者肯定知道,beego的路由设计来源于sinatra,原来是不支持自动路由的,每一个路由都要自己配置的,如: type MainController struct { beego. ...

  3. 学习KnockOut第三篇之List

    学习KnockOut第三篇之List 欲看此篇---------------------------------------------可先看上篇.          第一步,先搭建一个大概的框架起来 ...

  4. wap开发使用jquery mobile之后页面不加载外部css样式文件/js文件

    场景: wap开发,使用jquery mobile之后不会加载外部自定义的css文件了,需要手动刷新才会加载,查看外部自定义的js文件也是一样. 解决办法: 1.在page下面添加css样式,就不要写 ...

  5. About the Storage allocation

    It doesn't matter what programming language u use,it's all about the usage of variable---storage man ...

  6. SCI杂志更名时,如何计算影响因子?

  7. PHP读取xml之cdata讲解

    实例: xss.xml <?xml version="1.0" encoding="UTF-8"?><filters>    <f ...

  8. Kafka的消息格式

    Commit Log Kafka储存消息的文件被它叫做log,按照Kafka文档的说法是: Each partition is an ordered, immutable sequence of me ...

  9. HDU 1385 Minimum Transport Cost (最短路,并输出路径)

    题意:给你n个城市,一些城市之间会有一些道路,有边权.并且每个城市都会有一些费用. 然后你一些起点和终点,问你从起点到终点最少需要多少路途. 除了起点和终点,最短路的图中的每个城市的费用都要加上. 思 ...

  10. javascript 在一个函数参数中包含另一个函数的引用

    javascript函数的参数包含另一个函数的情形: <script> //b函数的参数func为另一个函数 function b(a, func) {  alert(a); //调用参数 ...