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. [基础] 重载的时候什么时候用引用&

    一般返回值还要继续被处理,而不仅仅是得到其值的时候,返回引用& 一般有[], =, ++, --, 还有输入输出运算符<<, >> Classtype &ope ...

  2. 意大利进口的衬衫面料pH值严重超标·都市快报

    意大利进口的衬衫面料pH值严重超标·都市快报     意大利进口的衬衫面料pH值严重超标         董捷    2007-03-24           通讯员 浙 检 记 者 董 捷      ...

  3. 解码美国传奇网络券商:TradeStation

    证券时报记者 桂衍民 张欣然 5万客户,交易量却占美国网络券商8%,网络影响力已连续两年被评为全美前五名,说起美国网络证券,必提TradeStation. TradeStation的确是美国证券界的一 ...

  4. Java NIO框架Netty demo

    Netty是什么 Netty是一个java开源框架.Netty提供异步的.事件驱动的网络应用程序框架和工具,用以快速开发高性能.高可靠性的网络服务器和客户端程序. 也就是说,Netty 是一个基于NI ...

  5. Android抓包工具Fiddler抓取数据

    1.手机端设置 2.Fiddler设置监听 Tools/Fiddler options 是否允许监听到https(Fiddler默认只抓取http格式的),首次点击会弹出是否信任fiddler证书和安 ...

  6. ZOJ 1301 The New Villa (BFS + 状态压缩)

    题意:黑先生新买了一栋别墅,可是里面的电灯线路的连接是很混乱的(每个房间的开关可能控制其他房间,房间数<=10),有一天晚上他回家时发现所有的灯(除了他出发的房间)都是关闭的,而他想回卧室去休息 ...

  7. 爱加密Android APk 原理解析

    转载请标明出处:http://blog.csdn.net/u011546655/article/details/45921025 爱加密Android APK加壳原理解析 一.什么是加壳? 加壳是在二 ...

  8. Android网络(4):HttpClient必经之路----使用线程安全的单例模式HttpClient,及HttpClient和Application的融合

    上文简介了HttpClient和Tomcatserver的交互,主角是HttpClient,然后它跟server交互有两种方式即get和post.所以这个HttpClient就相似于电脑上用的浏览器. ...

  9. 查看linux系统状态

    就类似你装完xp后,或者你拿到一台新的机器的时候,你通常都是进入系统,看看他的cpu,内存,硬盘使用情况.我也按照这个来看看linux的系统状态.1:top 退出按q,这个就类似windows的任务管 ...

  10. linux 获取系统屏幕分辨率

      在Windows下可以使用GetSystemMetrics(SM_CXSCREEN);GetSystemMetrics(SM_CYSCREEN) 获取. 在Linux下可以使用XDisplayWi ...