Treblecross is a two player game where the goal is to get three X in a row on a one-dimensional board. At the start of the game all cells in the board are empty. In each turn a player puts an X in an empty cell, and if the move results three X next to each other, that player wins.

Given the current state of the game, you are to determine if the current player to move can win the game assuming both players play optimally.

Consider the game where the board size is 5 cells. If the first player puts an X at position three (in the middle) so the state becomes ..X.., he will win the game as no matter where the other player puts his X, the first player can get three X in a row. If, on the other hand, the first player puts the X in any other position, the second player will win the game by putting the X in the opposite corner (for instance, after the second players move the state might be .X..X). This will force the first player to put an X in a position so the second player wins in the next move.

Input

Input starts with an integer T (≤ 200), denoting the number of test cases.

Each case starts with a line containing a string denoting the current status of the game. The string will only contain the characters '.' and'X'. The length of the string (the size of the board) will be between 3 and 200 characters, inclusive. No state will contain three X in a row.

Output

For each case, print the case number and the positions on the board, where the player to move may put an X and win the game. The positions should be separated by a single space, and be in increasing order. The leftmost position on the board is 1. If there is no such position print 0.

Sample Input

4

.....

X.....X..X.......X....X..X

.X.X...X

..................

Sample Output

Case 1: 3

Case 2: 0

Case 3: 3

Case 4: 5 6 13 14

 #include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
int sg[]= {},vi[];
int work(int x)
{
memset(vi,,sizeof(vi));
vi[sg[x-]]=,vi[sg[x-]]=;
int i;
for(i=; i<=x; i++)
vi[sg[x-i]^sg[i-]]=;
for(i=;; i++)
if(!vi[i])return i;
}
void init()
{
int i;
sg[]=,sg[]=,sg[]=,sg[]=;
for(i=; i<; i++)
{
sg[i]=work(i);
}
}
char a[];
int len;
int ans[],an;
bool check(int y)
{
int i,ok=;
for(i=; i<len; i++)
{
if(a[i]=='.')
{
if(i+<len&&a[i+]=='X'&&i+<len&&a[i+]=='X')
{
ok=;
if(y)
ans[an++]=i+;
}
else if(i->=&&a[i-]=='X'&&i->=&&a[i-]=='X')
{
ok=;
if(y)
ans[an++]=i+;
}
else if(i->=&&a[i-]=='X'&&i+<len&&a[i+]=='X')
{
ok=;
if(y)
ans[an++]=i+;
}
}
}
return ok;
}
bool check1()
{
int i=,j,k,ans=;
while(i<len)
{
while(i<len&&a[i]=='X')i++;
if(i==len)break;
j=i;
while(i<len&&a[i]=='.')i++;
k=i-j;
if(j)k-=;
if(i!=len)k-=;
if(k>=)
ans^=sg[k];
}
if(ans)ans=;
else ans=;
return ans;
}
void solve()
{
int i;
for(i=; i<len; i++)
{
if(a[i]=='.')
{
a[i]='X';
if(check())
{
a[i]='.';
continue;
}
if(check1())ans[an++]=i+;
a[i]='.';
}
}
}
int main()
{
init();
int t,i,cas=;
scanf("%d",&t);
while(t--)
{
an=;
scanf("%s",a);
printf("Case %d: ",cas++);
len=strlen(a);
if(!check())
solve();
if(an==)
{
printf("%d\n",);
}
else
{
printf("%d",ans[]);
for(i=; i<an; i++)
{
printf(" %d",ans[i]);
}
printf("\n");
}
}
}

Treblecross 博弈SG值的更多相关文章

  1. 【UVA1378】A Funny Stone Game (博弈-求SG值-输出方案)

    [题目] Description The funny stone game is coming. There are n piles of stones, numbered with 0, 1, 2, ...

  2. 博弈SG

    先转一篇看得比较懂的,以后有时间自己再归纳下 转自:http://blog.csdn.net/logic_nut/article/details/4711489 博弈问题若你想仔细学习博弈论,我强烈推 ...

  3. (博弈 sg入门2)

    接下来介绍Nim游戏(同样引用杭电上的,懒的打字) 1.有两个玩家:   2.  有三堆扑克牌(比如:可以分别是    5,7,9张):  3. 游戏双方轮流操作:  4. 玩家的每次操作是选择其中某 ...

  4. (转)博弈 SG函数

    此文为以下博客做的摘要: https://blog.csdn.net/strangedbly/article/details/51137432 ---------------------------- ...

  5. 尼姆博弈+SG函数

    博弈这个东西真的很费脑诶.. 尼姆博奕(Nim Game):游戏者轮流从一堆棋子(或者任何道具)中取走一个或者多个,最后不能再取的就是输家.当指定相应数量时,一堆这样的棋子称作一个尼姆堆 当n堆棋子的 ...

  6. 【转】博弈—SG函数

    转自:http://chensmiles.blog.163.com/blog/static/12146399120104644141326/ http://blog.csdn.net/xiaofeng ...

  7. HDU 1848 Fibonacci again and again (斐波那契博弈SG函数)

    Fibonacci again and again Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & ...

  8. Light OJ 1199 - Partitioning Game (博弈sg函数)

    D - Partitioning Game Time Limit:4000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu ...

  9. LightOJ 1315 - Game of Hyper Knights(博弈sg函数)

    G - Game of Hyper Knights Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%lld & ...

随机推荐

  1. [2014-09-21]如何在 Asp.net Mvc 开发过程中更好的使用Enum

    场景描述 在web开发过程中,有时候需要根据Enum类型生成下拉菜单: 有时候在输出枚举类型的时候,又希望输出对应的更具描述性的字符串. 喜欢直接用中文的请无视本文 不多说,直接看代码. 以下代码借鉴 ...

  2. 帆软的报表系统与泛微OA结合起来,这两个软件麦枫提供了经典的服务。

    一.集成配景泛微OA对企业的代价 泛微协同办公计划能向你供给一个协同的.集成的办公情况,使所有的办公职员都在统一且个性化的信息流派中一路事情, 解脱光阴和地区的限定,实现协同事情与知识治理. Eoco ...

  3. 在ubantu上安装hive

    上次我们在ubantu上安装了hadoop,那我们现在再进一步,开始我们的学习之旅--hive板块吧! 第一步:准备! 软件: I.一个搭建好的hadoop环境 II.hive的bin文件(前往apa ...

  4. SQL查询语句分类

    SQL查询语句有多种,下面总结下.首先先建三张表用于后面的实验 -- 学生表,记录学生信息 CREATE TABLE student( sno ), sname ), ssex ENUM('男','女 ...

  5. JS面向对象编程(进阶理解)

    JS 面向对象编程 如何创建JS对象 JSON语法声明对象(直接量声明对象) var obj = {}; 使用 Object 创建对象 var obj = new Object(); JS对象可以后期 ...

  6. win7系统盘扩容后不识别修复方法

    谢天谢地 终于在不重装系统的情况下,把C盘修复好了. win7系统,C盘之前是200G,使用中慢慢的就用完, 虽然把几乎所有的软件都移植到D盘了但是还是不能释放更多的空间.剩下60多G的空间,一般够用 ...

  7. [ASP.NET MVC]笔记(三) 成员资格、授权和安全性

    阻止CSRF(跨站请求伪造) ASP.NET MVC提供了一个阻止CSRF攻击的好方法 在每个提交的表单中包含 @using (Html.BeginForm("Index", &q ...

  8. iOS block和代理的区别

      block和代理是iOS开发中实现回调的两种方式,大多数情况下是用哪个都可以,主要看个人喜好.本文主要是对两者做一下对比. 1.block简介   在 iOS中, block一共分三种.   (1 ...

  9. IOS学习【前言】

    2016-1-14 16年开始时导师安排任务,开始IOS学习之旅  经过几天的学习,感觉还是需要作比较多的学习笔记,因此开始用博客记录整个过程,方便以后查看学习与分享. 主要记录一些关键的问题处理方法 ...

  10. Java线程:线程栈模型

    要理解线程调度的原理,以及线程执行过程,必须理解线程栈模型. 线程栈是指某时刻时内存中线程调度的栈信息,当前调用的方法总是位于栈顶.线程栈的内容是随着程序的运行动态变化的,因此研究线程栈必须选择一个运 ...