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. angularJS constant和value

    angularJS可以通过constant(name,value)和value(name,value)对于创建服务也是很重要的. 相同点是:都可以接受两个参数,name和value. 区别: 1.co ...

  2. MYSQL用户权限管理学习笔记

    MYSQL 用户管理 1.权限表 MYSQL是一个多用户的数据库,MYSQL的用户可以分为两大类: (1)       超级管理员用户(root),拥有全部权限 (2)       普通用户,由roo ...

  3. [Rails Level 2] Ground up

    1. Rails commends line: Example: rails new blog --skip-test-unit --database=postgresql

  4. STL——前闭后开区间表示法和function call 操作符

    前开后闭开区间表示法[) 任何一个STL算法,都需要获得由一对迭代器(泛型指针)所标示的区间,用以表示操作范围,这一对迭代器所标示的是个所谓的前闭后开区间,以[first,last)表示,也就是说,整 ...

  5. hook研究结果备忘

    hook研究结果: 最近一周时间仔细研究了一下hook,也许不能称之为研究吧.顶多是让别人的思想拿过来抄袭一遍而已,写点结果也算对得起自己的这几天的苦心了. 1,首先从同事旁边听到了hook,然后看的 ...

  6. oracle的sql优化

    http://www.cnblogs.com/rootq/archive/2008/11/17/1334727.html

  7. css渐变色

    <!DOCTYPE html><html><head> <meta http-equiv="content-type" content=& ...

  8. 12、SQL Server 行列转换

    SQL Server 行转列 在SQL Server 2005中PIVOT 用于将列值转换为列名(行转列),在SQL Server 2000中是没有这个关键字的 只能用case语句实现. --创建测试 ...

  9. htm5 user-scalable 的意思

    <meta name="viewport" content="width=device-width,user-scalable=yes,minimum-scale= ...

  10. Xcode快捷键 (本人总结常用的)

    以下分类显示最常用的快捷键: 1. 文件 CMD + N: 新文件 CMD + SHIFT + N:     新项目 CMD + O: 打开 CMD + S: 保存 CMD + SHIFT + S: ...