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 & ...
随机推荐
- pyhton安装pillow问题解决
最近在学习Python的微信处理相关,因wxpy库中的微信客户端需要接收二维码需要安装pillow,如下 class wxpy.Bot(cache_path=None, console_qr=Fals ...
- SQL菜鸟学习札记(二)
五月份一直在写SQL,之后写了一个期末大作业的项目,现在才有时间把之前遇到的各种奇怪的问题整理出来.下一部分札记应该是大作业中使用到的SQL的整理. 一.UPDATE SET语句后面可以并列赋值. 之 ...
- 学习js函数--函数定义
函数的定义方法有三种 1.函数表达式 2.函数声明 3,new function构造函数 这边主要看下函数表达式和函数声明 函数表达式(未省略标志的) var alertName = function ...
- 二、nginx搭建图片服务器
接上篇:Nginx安装手册 cd /usr/local/nginx/conf/ 配置图片服务器 方法一.在配置文件server{}中location /{} 修改配置: #默认请求 location ...
- Java内存模型:volatile详解
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt202 Java内存模型:volatile是干什么用的Volatile字段是用 ...
- SourceTree使用方法介绍
SourceTree比命令行更容易操作,能更直观看到发生了什么.但是没有哪一家git图形化软件能完成git的所有操作,封装后的使用也隐藏了git的一些细节,在图形化工具出现一些非常罕见的情况时,还是需 ...
- 201521123101 《Java程序设计》第6周学习总结
1. 本周学习总结 1.面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图,对面向对象思想进行一个总结. 2. 书面作业 1.clone方法 1.1 Object对象中 ...
- 201521123024 《Java程序设计》第2周学习总结
1. 本周学习总结 1.Scanner扫描器的用法 2.各种数据类型的使用 3.运算符和表达式的使用 4.枚举类.String类的应用 5.了解用package和import管理类别 2.书面作业 1 ...
- 201521123060《Java程序设计》第2周学习总结
1. 本周学习总结 a.进一步熟悉了Eclipse的使用和java程序的编写: b.学习了java数据的基本类型:整数类型,浮点类型等: c.学习了算数运算符,赋值运算符,位运算符,关系运算符,逻辑运 ...
- vbs读取excel的一个实例
功能:在excel中对ip与loginType这两列进行遍历读取.本程序依赖于excel文件的"sheet2"表单中具有这两列. dim dictTarget, objExcel ...