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 & ...
随机推荐
- python专题-函数式编程
函数式编程是使用一系列函数去解决问题,按照一般编程思维,面对问题时我们的思考方式是"怎么干",而函数函数式编程的思考方式是我要"干什么". 至于函数式编程的特点 ...
- 获取ip地址及城市信息
大家好,今天给大家分享的是一个简单的知识获取登录用户的ip地址及城市信息,lz是一个小白,如果有哪些错误的地方 欢迎大家指出 东西很简单,直接上代码 [HttpPost] public string ...
- jquery.tmplate使用心得
jquery.tmplate使用心得 jquery.tmpl.js,是与jquey共同使用的html模板插件.该插件可通过简单的语法将数据放入到html模板中,可以很好的将数据渲染到页面上.该插件在本 ...
- Phpstorm中使用SFTP
Phpstorm中经常会出现FTP连接失败的问题,这个时候我们可以使用SFTP来连接服务器. 1.添加服务器.tools--deployment--configuration/browse Remot ...
- 软件工程(GZSD2015)第二次作业进度
贵州师范大学软件工程第二次作业 徐 镇 王铭霞 张 英 涂江枫 张 燕 安 坤 周 娟 杨明颢 杨家堂 罗文豪 娄秀盛 周 娟 李盼 岳庆 张颖 李丽思 邓婷 唐洁 郑倩 尚清丽 陈小丽 毛茸 宋光能 ...
- 基于GUI的四则运算
基于GUI的四则运算 李志强 201421123028 连永刚 201421123014 林方言 201421123023 coding 地址 https://git.coding.net/lizhi ...
- 团队作业8——第二次项目冲刺(Beta阶段)--第四天
一.Daily Scrum Meeting照片 二.燃尽图 三.项目进展 学号 成员 贡献比 201421123001 廖婷婷 17% 201421123002 翁珊 18% 201421123004 ...
- 第06周-接口、内部类与Swing
1. 本周作业简评与建议 作业简评 Q1.覆盖clone需要:a.要implements标记接口 Cloneable接口.b.要区分浅拷贝与深拷贝.c.一般来说要调用super.clone,然后在此基 ...
- 201521123040 《Java程序设计》第6周学习总结
1.本章学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图,对面向对象思想进行一个总结. 2. 书面作业 clone方法1.1 Object对象中的c ...
- 201521123049 《JAVA程序设计》 第10周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常与多线程相关内容. 2. 书面作业 本次PTA作业题集异常.多线程 1.finally 题目4-2 1.1 截图你的提交结果(出 ...