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专题-Mysql数据库(python2._+ Mysqldb)
Python使用MySQL数据库 MySQLdb驱动从2014年1月停止了维护. Python2 MySQLdb 一,安装mysql 如果是windows 用户,mysql 的安装非常简单,直接下载安 ...
- python代码`if not x:` 和`if x is not None:`和`if not x is None:`
转自 http://blog.csdn.net/sasoritattoo/article/details/12451359 代码中经常会有变量是否为None的判断,有三种主要的写法: 第一种是`if ...
- Rxjava2视频笔记
观察者模式 观察者模式是一对多的模式,多个观察者同时监听一个主题,当主题更新时,会同时通知所有的观察者 subject –>observer 学习新技术的步骤 1.官方文档的查阅和学习 2.Go ...
- XMind入门教程
最近在总结一些框架知识的时候,总找不到一款好的软件来画流程图,后来在网上查找这方面的东西,找到了 XMind,发现用来画思维导图还挺好的,看起来思路清晰,美观.那么便将使用的一些经验分享给大家. 1. ...
- 『HTMl5』学习日志
w3g提供在线校验页面:http://validator.w3.org/ 1.文本框获取焦点 <%@ page language="java" import="ja ...
- POI设置excel添加列下拉框
POI在生成excel模板时需要为列添加下拉框,我写了两个方法. @ 方法一:适用任何情况,不受下拉框值数量限制.但是需要通过引用其它列值. 思路大概如下: 1.创建一个隐藏的sheet页,用于存放下 ...
- K/3 WISE V14.3官方原版云盘下载地址及培训PPT
电梯直达 楼主 发表于 15 分钟前 | 只看该作者 查看 : 10|回复 : 0 金蝶K/3 WISE打造企业"智造力",为企业财务管理决策提供全方位信息,产.供.销 ...
- django框架(Model)
-------------------使用MySql数据库-------------------1.进行对应mysql-python包的下载 pip install mysql-python 2.在m ...
- ActiveMQ笔记——技术点汇总
目录 · Introduction to ActiveMQ · Installing ActiveMQ · Message-oriented middleware · JMS specificatio ...
- 使用Spring实现读写分离( MySQL实现主从复制)
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt403 1. 背景 我们一般应用对数据库而言都是"读多写少&quo ...