Treblecross is a two player gamewhere the goal is to get three X in a row on a one-dimensional board. At the startof the game all cells in the board is empty. In each turn a player puts a X in an empty cell, and if that results in there beingthree X next to each other, that player wins.

Given the current state of the game, you are todetermine if the player to move can win the game assuming both players playperfectly. If so, you should also print all moves that will eventually lead toa win.

Consider the game where the board size is 5cells. If the first player puts a X at position three (in the middle) so thestate becomes ..X.., he will win the game as no matter where the other playerputs 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 winthe game by putting the X in the opposite corner (for instance, after thesecond player moves the state might be .X..X). This will force the first playerto put an X in a position so the second player wins in the next move.

Input

The input begins with an integer N ( N< 100),the number of states that will follow. Each state is represented by a string ona line by itself. The string will only contain the characters '.' and 'X'. Thelength of the string (the size of the board) will be between 3 and 200characters, inclusive. No state will contain three X in a row.

Output

For each case, first output WINNING or LOSING depending onif the player to move will win or lose the game. On the next line, output inincreasing order all positions on the board where the player to move may put anX and win the game. The positions should be separated by a blank, and be inincreasing order. The leftmost position on the board is 1.

  SampleInput                                                   Outputfor Sample Input

4

.....

X.....X..X.............X....X..X

.X.X...X

...............................................

WINNING

3

LOSING

 

WINNING

3

WINNING

1 12 15 17 20 24 28 31 33 36 47

 

题意:给定一个串,上面有'X'和'.',可以在'.'的位置放X,谁先放出3个'X'就赢了,求先手必胜的策略

题解:SG函数,每个串要是上面有一个X,周围的4个位置就是禁区了(放下去必败),所以可以以X分为几个子游戏去求SG函数的异或和进行判断,至于求策略,就是枚举每个位置就可以了

#include<bits/stdc++.h>
#define N 250
#define mes(x) memset(x, 0, sizeof(x));
#define ll __int64
const long long mod = 1e9+;
const int MAX = 0x7ffffff;
using namespace std;
int SG[N], dir[N], a[N], l;
char s[N];
int f(){
int num, i, ans;
for(ans=num=i=;i<=l;i++)
if(!a[i]) num++;
else{
ans ^= SG[num];
num = ;
}
return ans;
}
void getsg(){
int n, x, j;
SG[] = ;
SG[] = SG[] = SG[] = ;
for(n=;n<=;n++){
mes(dir);
for(x=n-;x<=n-;x++)
if(x>=) dir[SG[x]] = ;
for(x=;n--x>=;x++)
dir[SG[x]^SG[n--x]] = ;
for(j=;;j++){
if(!dir[j]){
SG[n] = j;
break;
}
}
}
}
int main()
{
int T, i, flag, j;
getsg();
scanf("%d", &T);
while(T--){
scanf("%s", s);
l = strlen(s);
if(strstr(s,"X.X")||strstr(s, "XX")){
mes(dir);
printf("WINNING\n");
for(i=;i<l-;i++){
if(s[i] == 'X'&&s[i+] == 'X') dir[i+] = ;
if(s[i] == 'X'&&s[i+] == 'X'){
dir[i+] = ;
if(i >= )
dir[i-] = ;
}
}
if(s[l-] == 'X'&& s[l-] == 'X')
dir[l-] = ;
for(flag=i=;i<;i++)
if(dir[i]){
printf("%s%d", flag?" ":"", i+);
flag = ;
}
printf("\n");
}
else{
mes(dir);
for(i=;i<l;i++)
if(s[i] == 'X')
for(j=i-;j<=i+;j++)
if(j<l&&j>=) dir[j] = ;
memcpy(a, dir, sizeof(a));
a[l] = ;
if(!f()){
printf("LOSING\n\n");
continue;
}
printf("WINNING\n");
for(flag=,i=;i<l;i++){
memcpy(a, dir, sizeof(a));
a[l] = ;
if(!dir[i]){
for(j=i-;j<=i+;j++)
if(j<l&&j>=) a[j] = ;
if(!f()){
printf("%s%d", flag?"":" ", i+);
flag = ;
}
}
}
printf("\n");
}
}
}

UVA10561 Treblecross 组合游戏/SG定理的更多相关文章

  1. 组合游戏 - SG函数和SG定理

    在介绍SG函数和SG定理之前我们先介绍介绍必胜点与必败点吧. 必胜点和必败点的概念:        P点:必败点,换而言之,就是谁处于此位置,则在双方操作正确的情况下必败.        N点:必胜点 ...

  2. HDU 1536 S-Nim (组合游戏+SG函数)

    题意:针对Nim博弈,给定上一个集合,然后下面有 m 个询问,每个询问有 x 堆石子 ,问你每次只能从某一个堆中取出 y 个石子,并且这个 y 必须属于给定的集合,问你先手胜还是负. 析:一个很简单的 ...

  3. hdu 3980 Paint Chain 组合游戏 SG函数

    题目链接 题意 有一个\(n\)个珠子的环,两人轮流给环上的珠子涂色.规定每次涂色必须涂连续的\(m\)颗珠子,无法继续操作的人输.问先手能否赢. 思路 参考 转化 第一个人取完之后就变成了一条链,现 ...

  4. hdu 1848 Fibonacci again and again 组合游戏 SG函数

    题目链接 题意 三堆石子,分别为\(m,n,p\)个,两人依次取石子,每次只能在一堆当中取,并且取的个数只能是斐波那契数.最后没石子可取的人为负.问先手会赢还是会输? 思路 直接按定义计算\(SG\) ...

  5. 博弈论题目总结(二)——SG组合游戏及变形

    SG函数 为了更一般化博弈问题,我们引入SG函数 SG函数有如下性质: 1.如果某个状态SG函数值为0,则它后继的每个状态SG函数值都不为0 2.如果某个状态SG函数值不为0,则它至少存在一个后继的状 ...

  6. 【博弈论】组合游戏及SG函数浅析

    目录 预备知识 普通的Nim游戏 SG函数 预备知识 公平组合游戏(ICG) 若一个游戏满足: 由两名玩家交替行动: 游戏中任意时刻,合法操作集合只取决于这个局面本身: 若轮到某位选手时,若该选手无合 ...

  7. UOJ 266 - 【清华集训2016】Alice和Bob又在玩游戏(SG 定理+01-trie)

    题面传送门 神仙题. 首先注意到此题的游戏是一个 ICG,故考虑使用 SG 定理解决这个题,显然我们只需对每个连通块计算一遍其 SG 值异或起来检验是否非零即可.注意到我们每删除一个点到根节点的路径后 ...

  8. HDU 1851 (巴什博奕 SG定理) A Simple Game

    这是由n个巴什博奕的游戏合成的组合游戏. 对于一个有m个石子,每次至多取l个的巴什博奕,这个状态的SG函数值为m % (l + 1). 然后根据SG定理,合成游戏的SG函数就是各个子游戏SG函数值的异 ...

  9. SG函数和SG定理【详解】

    在介绍SG函数和SG定理之前我们先介绍介绍必胜点与必败点吧. 必胜点和必败点的概念:        P点:必败点,换而言之,就是谁处于此位置,则在双方操作正确的情况下必败.        N点:必胜点 ...

随机推荐

  1. java数据结构

    1.计算机科技的两大支柱 1.数据结构 2.算法 程序=数据结构+算法 2.定义:是一门研究非数值计算的程序设计问题中计算机的操作对象以及它们之间的关系和操作等等的学科 3.数据(Data):是对信息 ...

  2. Windows内核 基本汇编指令

    1)用VS2010新建Win32 Console Application,工程名为ACECore,工程建立完成后得到打开文件ACECore.cpp,代码如下: #include "stdaf ...

  3. 精通JavaScript的this关键字

    小提示 阅读本文,您需要了解JS的基本常识.您将花费40分钟完成本文的阅读. JS中的this关键字让很多新老JS开发人员都感到困惑.这篇文章将对this关键字进行完整地阐述.读完本文以后,您的困惑将 ...

  4. chmod() has been disabled for security reasons

    最近用 codeigniter 写一个小系统,引用了session 库,codeigniter默认的session存储方式为files.鉴于安全性,文件即肯定涉及到权限问题. 在类 UNIX 操作系统 ...

  5. JAVA CAS原理

    转自: http://blog.csdn.net/hsuxu/article/details/9467651 CAS CAS: Compare and Swap java.util.concurren ...

  6. http://www.cnblogs.com/softidea/p/5631763.html

    http://www.cnblogs.com/softidea/p/5631763.html

  7. CentOS6.5安装配置SVN

    安装SVN软件包[root@localhost ~]# yum install subversion#确认是否已安装svn模块[root@localhost ~]# cd /etc/httpd/mod ...

  8. Top (参数)

    最近在优化数据库服务器上高消耗语句/过程,发现一个存储过程优化后依旧出现在Profiler跟踪里.将Profiler跟踪文件中过程执行语句取出,打开一个查询窗口(SPID=144),set stati ...

  9. php截取字符串

    1.substr(源字符串,其实位置[,长度])-截取字符串返回部分字符串 <?php $str ="phpddt.com"; echo substr($str, 2); / ...

  10. oracle数据导出工具sqluldr2

    oracle数据导出工具sqluldr2可以将数据以csv.txt等格式导出,适用于大批量数据的导出,导出速度非常快.导出后可以使用oracle loader工具将数据导入.下载完sqluldr2,工 ...