uva10561 - Treblecross
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的更多相关文章
- UVA10561 Treblecross 组合游戏/SG定理
Treblecross is a two player gamewhere the goal is to get three X in a row on a one-dimensional board ...
- UVA10561 Treblecross —— SG博弈
题目链接:https://vjudge.net/problem/UVA-10561 题意: 两个人玩游戏,轮流操作:每次往里面添加一个X,第一个得到XXX的获胜. 题解: 详情请看<训练指南&g ...
- 【UVA10561】Treblecross(SG函数)
题意:有n个格子排成一行,其中一些格子里面有字符X.两个游戏者轮流操作,每次可以选一个空格,在里面放上字符X. 如果此时有3个连续的X出现,则该游戏者赢得比赛.初始条件下不会有3个X连续出现. 判断先 ...
- Treblecross 博弈SG值
Treblecross is a two player game where the goal is to get three X in a row on a one-dimensional boar ...
- UVA 10561 - Treblecross(博弈SG函数)
UVA 10561 - Treblecross 题目链接 题意:给定一个串,上面有'X'和'.',能够在'.'的位置放X.谁先放出3个'X'就赢了,求先手必胜的策略 思路:SG函数,每一个串要是上面有 ...
- UVa 10561 - Treblecross
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- UVa 10561 (SG函数 递推) Treblecross
如果已经有三个相邻的X,则先手已经输了. 如果有两个相邻的X或者两个X相隔一个.,那么先手一定胜. 除去上面两种情况,每个X周围两个格子不能再放X了,因为放完之后,对手下一轮再放一个就输了. 最后当“ ...
- UVA 10561 Treblecross(博弈论)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=32209 [思路] 博弈论. 根据X分布划分禁区,每个可以放置的块为 ...
- 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 ...
随机推荐
- POJ 1456 Supermarket
题意:商场卖东西,每种商品有两个属性,一种是价格pi,另一种是保质期di,每种商品只能在天数<=di的时候卖出.每天只能卖一种商品,问最多能卖出价格之和为多少的商品.(n <= 10^4, ...
- 嵌套fragment时必须要重写 onDetach()
/** * 嵌套fragment时必须要重写 onDetach()如下 */ @Override public void onDetach() { super.onDetach(); ...
- jquery常用见的正则表达式
quickexpr = /^(?:[^<]*(<[ww]+>)[^>]*$|#([w-]+)$)/ (?:…)表示是一个非捕获型 [^<]表示是以"<& ...
- Qt Quick 事件处理之信号与槽
前面两篇文章<QML 语言基础>和<Qt Quick 简单教程>中我们介绍了 QML 语言的基本的语法和 Qt Quick 的常见元素,亲们,通过这两篇文章,您应该已经能够完毕 ...
- mongoDB的基本使用----飞天博客
Mongo的介绍:这个mongoDB官网说的好啊,MongoDB是一个开源的基于document的数据库,并且是优秀的NoSQL数据库,并且它是用C++写滴哈,非常有效率.一些什么特点呢? 全索引支持 ...
- Swoole源代码学习记录(十五)——Timer模块分析
swoole版本号:1.7.7-stable Github地址:点此查看 1.Timer 1.1.swTimer_interval_node 声明: // swoole.h 1045-1050h ty ...
- ViewPager切换大量Fragment不刷新的问题
PagerAdapter,需要重写instantiateItem()加载视图,onDestroy()销毁视图FragmentPagerAdapter,每一个生成的Fargment都保存在内存中,也就是 ...
- 改写URL的查询字符串QUERY_STRING(转)
查询字符串是指URL请求中“问号”后面的部分.比如,http://www.nowamagic.net/?foo=bar中粗体部分就是查询字符串,其中变量名是foo,值是bar. 1. 利用QSA转换查 ...
- [转] Javascript中数组与字典(即object)的使用
简述: 简单记录一下数据结构字典和数组, 其实在Javascript这种弱类型的脚本语言中,数组同时也就是字典,下面主要就是字典数组的简易使用 代码: 1. 数组中添加map <!DOCTYPE ...
- Linux磁盘管理:lvcreate 常用命令
查看当前LV及PV信息: [root@rusky ~]# hostnamectl Static hostname: localhost.localdomain Transient hostname: ...