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 is empty. In each turn a player puts a `X' in an empty
cell, and if that results in there being three `X' next to each other, that player wins.
Given the current state of the game, you are to determine if the player to move can win the game
assuming both players play perfectly. If so, you should also print all moves that will eventually lead to
a win.
Consider the game where the board size is 5 cells. If the rst player puts a `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 rst player can get three `X' in a row. If, on the other hand, the rst 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 player moves the state might be `.X..X'). This will force the rst player
to 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 on a line by itself. 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, rst output `WINNING' or `LOSING' depending on if the player to move will win or lose the
game. On the next line, output in increasing order all positions on the board where the player to move
may put an X and win the game. The positions should be separated by a blank, and be in increasing
order. The leftmost position on the board is 1.
Sample Input
4
.....
X.....X..X.............X....X..X
.X.X...X
...............................................
Sample Output
WINNING
3
LOSING
WINNING
3
WINNING
1 12 15 17 20 24 28 31 33 36 47

白书上的原题,注意此题细节非常多。

code:

 #include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#define maxn 205
using namespace std;
char ch,s[maxn];
int ti,n,sg[maxn],cnt,l,idx,tmp;
bool ok,bo[maxn*maxn],exist[maxn],flag,first;
struct DATA{
int l,r,siz;
}list[maxn];
void read(int &x){
for (ok=,ch=getchar();!isdigit(ch);ch=getchar()) if (ch=='-') ok=;
for (x=;isdigit(ch);x=x*+ch-'',ch=getchar());
if (ok) x=-x;
}
void calc(int k){
for (int i=,t1,t2;i<=k;i++){
t1=(i-)-,t2=k-(i+);
if (t1>&&sg[t1]==-) calc(t1);
if (t2>&&sg[t2]==-) calc(t2);
}
memset(bo,,sizeof(bo));
for (int i=,t1,t2,t;i<=k;i++){
t=,t1=(i-)-,t2=k-(i+);
if (t1>) t^=sg[t1];
if (t2>) t^=sg[t2];
bo[t]=;
}
for (int i=;;i++) if (!bo[i]){sg[k]=i;break;}
}
void prepare(){
memset(sg,-,sizeof(sg));
for (int i=;i>=;i--) if (sg[i]==-) calc(i);
}
void write(int x){
if (first) first=;
else putchar(' ');
printf("%d",x);
}
int main(){
prepare();
for (read(ti);ti;ti--){
memset(s,,sizeof(s));
scanf("%s",s+);
n=strlen(s+),flag=,idx=cnt=tmp=,l=(s[]=='X'?:);
memset(list,,sizeof(list));
for (int i=;i<=n;i++)
if (s[i]=='X'){
if (s[i+]=='X'||s[i+]=='X'){flag=;break;}
if ((i-)-l>) list[++idx]=(DATA){l,i-,i--l};
l=i+;
}
if (l<=n) list[++idx]=(DATA){l,n,n-l+};
if (flag){
puts("WINNING");
memset(exist,,sizeof(exist)); first=;
for (int i=;i<=n;i++)
if (s[i]=='X'){
if (s[i+]=='X'){
if (i->=&&!exist[i-]) write(i-),exist[i-]=;
if (i+<=n&&!exist[i+]) write(i+),exist[i+]=;
}
if (s[i+]=='X'){
if (i+<=n&&!exist[i+]) write(i+),exist[i+]=;
}
}
puts("");
continue;
}
for (int i=;i<=idx;i++) tmp^=sg[list[i].siz];
if (tmp){
puts("WINNING");
first=;
for (int i=,t;i<=idx;i++){
t=sg[list[i].siz];
for (int j=list[i].l,t1,t2,t3;j<=list[i].r;j++){
t1=(j-)-list[i].l,t2=list[i].r-(j+),t3=;
if (t1) t3^=sg[t1];
if (t2) t3^=sg[t2];
if (!(tmp^t^t3)) write(j);
}
}
puts("");
continue;
}
else puts("LOSING"),puts("");
}
return ;
}

uva10561 - Treblecross的更多相关文章

  1. UVA10561 Treblecross 组合游戏/SG定理

    Treblecross is a two player gamewhere the goal is to get three X in a row on a one-dimensional board ...

  2. UVA10561 Treblecross —— SG博弈

    题目链接:https://vjudge.net/problem/UVA-10561 题意: 两个人玩游戏,轮流操作:每次往里面添加一个X,第一个得到XXX的获胜. 题解: 详情请看<训练指南&g ...

  3. 【UVA10561】Treblecross(SG函数)

    题意:有n个格子排成一行,其中一些格子里面有字符X.两个游戏者轮流操作,每次可以选一个空格,在里面放上字符X. 如果此时有3个连续的X出现,则该游戏者赢得比赛.初始条件下不会有3个X连续出现. 判断先 ...

  4. Treblecross 博弈SG值

    Treblecross is a two player game where the goal is to get three X in a row on a one-dimensional boar ...

  5. UVA 10561 - Treblecross(博弈SG函数)

    UVA 10561 - Treblecross 题目链接 题意:给定一个串,上面有'X'和'.',能够在'.'的位置放X.谁先放出3个'X'就赢了,求先手必胜的策略 思路:SG函数,每一个串要是上面有 ...

  6. UVa 10561 - Treblecross

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  7. UVa 10561 (SG函数 递推) Treblecross

    如果已经有三个相邻的X,则先手已经输了. 如果有两个相邻的X或者两个X相隔一个.,那么先手一定胜. 除去上面两种情况,每个X周围两个格子不能再放X了,因为放完之后,对手下一轮再放一个就输了. 最后当“ ...

  8. UVA 10561 Treblecross(博弈论)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=32209 [思路] 博弈论. 根据X分布划分禁区,每个可以放置的块为 ...

  9. fzu1969 GCD Extreme 类似于uva10561

    Description Given the value of N, you will have to find the value of G. The meaning of G is given in ...

随机推荐

  1. Nightmare(搜索)

    http://acm.hdu.edu.cn/showproblem.php?pid=1072 /* 题意: 迷宫内有入口和出口 在6分钟结束后炸弹会爆炸,但是迷宫内有重置炸弹的装置,可以重置炸弹的时间 ...

  2. [经典] 在未排序数组中返回topK大的数

    解法一,排序 先从大到小快排,然后扫前K个返回 时间复杂度:O(NlogN),空间复杂度O(1) 解法二,优先队列 前K个放入优先队列中,与最小堆顶元素比较大小,若大于则删除堆顶并插入:否则跳过 时间 ...

  3. SRM 392(1-250pt)

    DIV1 250pt 题意:给两个各含有一个*号的字符串s1和s2,可以用一个任意字符串代替*号(注意是串,不是只能用单个字符代替,也可以为用空串代替),问能否将s1和s2变为相同的字符串.如果能输出 ...

  4. O - Extended Traffic(判断负环)

    题意:有n个城市,每一个城市有一个拥挤度ai,从一个城市I到另一个城市J的时间为:(aJ-aI)^3,存在负环.问从第一个城市到达第k个城市所话的时间,如果不能到达,或者时间小于3输出?否则输出所花的 ...

  5. Django教程:第一个Django应用程序(3)

    Django教程:第一个Django应用程序(3) 2013-10-08 磁针石 #承接软件自动化实施与培训等gtalk:ouyangchongwu#gmail.comqq 37391319 #博客: ...

  6. linux Tomcat restart脚本简单版

    linux系统下重启tomcat的shell脚本: tomcat_home=/opt/apache-tomcat-6.0.32  #找到tomcat进程的id并kill掉 ps -ef |grep t ...

  7. IOS UIButton使用详解

    第一.UIButton的定义 UIButton *button=[[UIButton buttonWithType:(UIButtonType); 能够定义的button类型有以下6种, typede ...

  8. 工作于内存和文件之间的页缓存, Page Cache, the Affair Between Memory and Files

    原文作者:Gustavo Duarte 原文地址:http://duartes.org/gustavo/blog/post/what-your-computer-does-while-you-wait ...

  9. Win7下Qt5.2中使用OpenGL的glu函数库无法使用的解决方案

          最近在Window7使用Qt5.2学习OpenGL时,出现了以OpenGL中glu开头的函数库无法使用的错误,例如: 'gluPerspective'  was not declared ...

  10. [转] C++虚函数与虚函数表

    http://www.cnblogs.com/Ripper-Y/archive/2012/05/15/2501930.html http://blog.csdn.net/haoel/article/d ...