如果已经有三个相邻的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. Backbone.Events—纯净MVC框架的双向绑定基石

    Backbone.Events-纯净MVC框架的双向绑定基石 为什么Backbone是纯净MVC? 在这个大前端时代,各路MV*框架如雨后春笋搬涌现出来,在infoQ上有一篇 12种JavaScrip ...

  2. ACCESS数据库C#操作类(包含事务)

    转自http://blog.csdn.net/allen3010/article/details/6336717 这个是针对ACCESS数据库操作的类,同样也是从SQLHELPER提取而来,分页程序的 ...

  3. bug集合

    解决方法:vertical-align:top;  垂直对齐方式:对浮动元素无效浮动类: ie 6 7要在一行显示多个div要 给每一个元素浮动 否则会出间隙. ie6双倍边距bug 1. bug条件 ...

  4. EXT4.2--Ext Designer 使用

    前言: “画EXT”是一个美好的想法,如果有一款可视化工具能够只需进行拖拽而设计EXT,生成代码--那真是一件美丽的事.然而现实是,即使是为Eclipse装上EXT插件,用上idea,手写代码的提示也 ...

  5. 输入n个整数,找出其中最小的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,。

    // ConsoleApplication2.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include "stdafx.h ...

  6. Unity3D研究院之打开Activity与调用JAVA代码传递参数

    原地址:http://www.xuanyusong.com/archives/667    Unity for Android 比较特殊,Unity for IOS 打包是将XCODE工程直接交给开发 ...

  7. jsp bean标签

    jsp中存在一个奇奇怪怪的bean标签. 例如 现在在java包中定义一个类test2 package bean; public class test { private int number; pu ...

  8. Javascript操作表格隔行变色

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

  9. node.js 安装、图文详解

    网上的教程很多,有些模糊不清,有些版本太旧,有些是.exe安装,本文讲述windows系统下简单nodejs .msi环境配置.最新版是Current version: v0.10.26.官网下载地址 ...

  10. lintcode 中等题:partition array 数组划分

    题目 数组划分 给出一个整数数组nums和一个整数k.划分数组(即移动数组nums中的元素),使得: 所有小于k的元素移到左边 所有大于等于k的元素移到右边 返回数组划分的位置,即数组中第一个位置i, ...