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. nyoj-71

    描述 进行一次独木舟的旅行活动,独木舟可以在港口租到,并且之间没有区别.一条独木舟最多只能乘坐两个人,且乘客的总重量不能超过独木舟的最大承载量.我们要尽量减少这次活动中的花销,所以要找出可以安置所有旅 ...

  2. Struts2的流程(三)

    Struts的流程图如下(需要完全理解):

  3. 利用box-flex实现 dom元素位置页面底部

    问题: 总是有这样的需求,就是页面上某部分要位于页面的最底部,此“最底部”要求:(1)当页面上内容不足一屏的时候,在最底部显示(2)当页面上内容不止一屏的时候,也就是有垂直滚动条的时候,要在内容的最后 ...

  4. IOS第八天(6:UITableViewController新浪微博, 模型和 控件位置封装一起statusFrame)

    *****HMViewController #import "HMViewController.h" #import "HMStatus.h" #import ...

  5. buffer pool

    https://dev.mysql.com/doc/refman/5.5/en/glossary.html#glos_buffer_pool buffer pool The memory area t ...

  6. laravel redis

    安装配置redis服务器 $ wget http://download.redis.io/releases/redis-3.0.5.tar.gz $ tar xzf redis-.tar.gz $ c ...

  7. AFNetworking的原理与基本使用

    全称是AFNetworking 虽然运行效率没有ASI高,但是使用比ASI简单 是对NSURLConnection和NSURLSession的各自的一层包装 AFN的内部中的RunLoop AFN内部 ...

  8. Rotate List || LeetCode

    /** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * } ...

  9. 解决 git 中文路径显示 unicode 代码的问题

    解决 git 中文路径显示 unicode 代码的问题 当被修改的文件中带有中文字符时,中文字符会被转换为 unicode 代码,看不出原来的文件名. 这时,只要配置 :: git config -- ...

  10. 忘记Linux root用户的密码怎么办?

    以前忘记windows密码的时候,要么用工具清除,要么重装系统.假如你忘记了linux系统的root密码,怎么办呢?是像windows一样用工具破解还是重装系统呢?哈哈,都不用.这方法很简单,现在做一 ...