Treblecross 博弈SG值
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值的更多相关文章
- 【UVA1378】A Funny Stone Game (博弈-求SG值-输出方案)
[题目] Description The funny stone game is coming. There are n piles of stones, numbered with 0, 1, 2, ...
- 博弈SG
先转一篇看得比较懂的,以后有时间自己再归纳下 转自:http://blog.csdn.net/logic_nut/article/details/4711489 博弈问题若你想仔细学习博弈论,我强烈推 ...
- (博弈 sg入门2)
接下来介绍Nim游戏(同样引用杭电上的,懒的打字) 1.有两个玩家: 2. 有三堆扑克牌(比如:可以分别是 5,7,9张): 3. 游戏双方轮流操作: 4. 玩家的每次操作是选择其中某 ...
- (转)博弈 SG函数
此文为以下博客做的摘要: https://blog.csdn.net/strangedbly/article/details/51137432 ---------------------------- ...
- 尼姆博弈+SG函数
博弈这个东西真的很费脑诶.. 尼姆博奕(Nim Game):游戏者轮流从一堆棋子(或者任何道具)中取走一个或者多个,最后不能再取的就是输家.当指定相应数量时,一堆这样的棋子称作一个尼姆堆 当n堆棋子的 ...
- 【转】博弈—SG函数
转自:http://chensmiles.blog.163.com/blog/static/12146399120104644141326/ http://blog.csdn.net/xiaofeng ...
- HDU 1848 Fibonacci again and again (斐波那契博弈SG函数)
Fibonacci again and again Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & ...
- Light OJ 1199 - Partitioning Game (博弈sg函数)
D - Partitioning Game Time Limit:4000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu ...
- LightOJ 1315 - Game of Hyper Knights(博弈sg函数)
G - Game of Hyper Knights Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%lld & ...
随机推荐
- web安全:QQ号快速登录漏洞及被盗原理
为什么你什么都没干,但QQ空间中却发了很多小广告?也许你的QQ账号已经被盗.本文将讲解一个QQ的快速登录的漏洞. 我前阵子在论坛上看到一个QQ的快速登录的漏洞,觉得非常不错,所以把部分原文给转到园子来 ...
- OpenID Connect:OAuth 2.0协议之上的简单身份层
OpenID Connect是什么?OpenID Connect(目前版本是1.0)是OAuth 2.0协议(可参考本人此篇:OAuth 2.0 / RCF6749 协议解读)之上的简单身份层,用 A ...
- Core Java 谈谈HashMap
说起Java的HashMap相信大家都不是很陌生,但是对于HashMap内部结构有些同学可能不太了解,咱们下一步就将其展开. HashMap是基于Hash算法的,同理的还有HashSet和HashTa ...
- Apache配置网站根目录
Apache是世界使用排名第一的Web服务器软件.它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件之一. 在安装 Apache 时,系统会给定一 ...
- 201521123091 《Java程序设计》第6周学习总结
Java 第六周总结 第六周的作业. 目录 1.本章学习总结 2.Java Q&A 3.码云上代码提交记录及PTA实验总结 1.本章学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以 ...
- 201521123037 《Java程序设计》第5周学习总结
1. 本周学习总结 1.1 尝试使用思维导图总结有关多态与接口的知识点. 1.2 可选:使用常规方法总结其他上课内容. 接口: 接口简而言之是方法声明和常量值的集合,接口中所有的方法默认为public ...
- 201521123053 《Java程序设计》第5周学习总结
1. 本周学习总结 1.1 尝试使用思维导图总结有关多态与接口的知识点. 1.2 可选:使用常规方法总结其他上课内容. 答:我开始做笔记了,在本周学习中的一些笔记 * abstract关键字是为了实现 ...
- 201521123121 《Java程序设计》第4周学习总结
1. 本周学习总结 1.1 尝试使用思维导图总结有关继承的知识点. 1.2 使用常规方法总结其他上课内容. 对象的封装:将类的某些信息隐藏在类内部,不允许外部程序直接访问,而是通过该类提供的方法来实现 ...
- 201521123036 《Java程序设计》第3周学习总结
本周学习总结 初学面向对象,会学习到很多碎片化的概念与知识.尝试学会使用思维导图将这些碎片化的概念.知识组织起来. 书面作业 Q1:代码阅读 public class Test1 { private ...
- 201521123055 《Java程序设计》第12周学习总结
1. 本章学习总结 2. 书面作业 将Student对象(属性:int id, String name,int age,double grade)写入文件student.data.从文件读出显示. Q ...