如果已经有三个相邻的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. JQuery,UIbootstrap风格弹出层

    <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head> <met ...

  2. nenu contest2

    http://vjudge.net/vjudge/contest/view.action?cid=54562#overview H  B. Polygons http://codeforces.com ...

  3. 无废话网页重构系列——(6)HTML主干结构:站点(site)、页面(page)

    本文作者:大象本文地址:http://www.cnblogs.com/daxiang/p/4653546.html 在分析和切出设计稿,以及部署项目目录文件后,开始写HTML Demo. 首先,弄出H ...

  4. MATLAB三维散点图的绘制(scatter3、plot3)

    MATLAB三维散点图的绘制(scatter3.plot3) (1)函数scatter3 用法:scatter3(x,y,z,'.',c) % c 为颜色,需和x,y,z长度相同 例子: x=[422 ...

  5. 仪表盘 hostmap 新玩法让运维工作越玩越 high

    Cloud Insight 第13次新品发布会现在开始,首先非常感谢大家前来看我们的新功能发布会,下面我先给大家介绍一下新功能,之后有什么问题大家尽管问

  6. Observer Pattern

    Motivation We can not talk about Object Oriented Programming without considering the state of the ob ...

  7. Searching a 2D Sorted Matrix Part I

    Write an efficient algorithm that searches for a value in an n x m table (two-dimensional array). Th ...

  8. poj 3625 Building Roads(最小生成树,二维坐标,基础)

    题目 //最小生成树,只是变成二维的了 #define _CRT_SECURE_NO_WARNINGS #include<stdlib.h> #include<stdio.h> ...

  9. autocomplete参数说明以及实例

    JQuery autocomplete使用手册 Jquery autocomplete是一个很强大的类似google suggest的自动提示插件.它几乎可以满足我们所有的需要. 官方网站:http: ...

  10. ifram一些常用的知识点

    本文摘自:http://www.cnblogs.com/duankaige/archive/2012/09/20/2695012.html   iframe的调用包括以下几个方面:(调用包含html ...