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. Maven 中央仓库及阿里云仓库地址

    Maven 中央仓库地址: 1. http://www.sonatype.org/nexus/ 2. http://mvnrepository.com/ 3. http://repo1.maven.o ...

  2. poj 1742 多重背包

    题意:给出n种面值的硬币, 和这些硬币每一种的数量, 要求求出能组成的钱数(小于等于m) 思路:一开始直接用多重背包套上去超时了,然后就没辙了,然后参考网上的,说只需要判断是否能取到就行了,并不需要记 ...

  3. OV7670读操作

    读时序共分为五个部分 首先发送start,然后发送OV7670的器件地址,ov6070的ID是0x42,0x42+一位响应位 发送ov7670的寄存器地址,这里可以读取它的厂商识别号 ,比如1c  发 ...

  4. CCNA基础知识摘录

    cisco设备的启动要点: 1.检测硬件(保存在rom) 2.载入软件(IOS)(保存在Flash) 3.调入配置文件(密码,IP地址,路由协议都保存在此)(此文件保存在NVRAM) 0x2102:正 ...

  5. MPLS VPN随堂笔记1

    MPLS VPN 基础 1.MPLS vpn架构的特点 1.1.允许不同CE传递相同私网路由 1.2.SP内部(所有P路由器)不需要学习CE路由 1.3.无安全保障但有带宽保障(跟SP租用服务) 2. ...

  6. 【集美大学1411_助教博客】团队作业1——团队展示&选题 成绩

    第一次团队作业已经新鲜出炉啦,各位同学请查收.截止日期前,全班都按时提交了作业,而且有的团队还提交了两次呢,下次不要这样啦~ 题目 团队作业1--团队展示&选题 回顾 个人作业1--四则运算题 ...

  7. 201521123083 《Java程序设计》第7周学习总结

    1. 本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 参考资料: 2. 书面作业 1. ArrayList代码分析 1.1 解释ArrayList的contains public ...

  8. Quartz2.2.x官方教程

    零.Quartz是什么?能干什么? Quartz是一个开源的任务调度框架.基于定时.定期的策略来执行任务是它的核心功能,比如x年x月的每个星期五上午8点到9点,每隔10分钟执行1次.Quartz有3个 ...

  9. 201521123040《Java程序设计》第4周学习总结

    1. 本周学习总结 1.1 尝试使用思维导图总结有关继承的知识点. 1.2 使用常规方法总结其他上课内容. 本周学习了类的继承,包括子类父类的概念还有extends关键字,super关键字.继承与代码 ...

  10. 201521123037 《Java程序设计》第11周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多线程相关内容. Java多线程之Executor.ExecutorService.Executors.Callable.Futur ...