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 ...
随机推荐
- tcpCopy
tcpcopy是一种应用请求复制(基于tcp的packets)工具,其应用领域较广,我们曾经应用于网易的广告投放系统,urs系统,nginx hmux协议开发等系统,避免了上线带来的很多问题. 总体说 ...
- Genymotion开始搞起~
简介 一:什么是GenymotionGenymotion是一款完全超越BlueStacks的安卓模拟器,正如它中文官网的介绍:快到极致的Android模拟器.英文官网:http://www.genym ...
- 《Linux命令行与shell脚本编程大全》 第二十七章 学习笔记
第二十七章:shell脚本编程进阶 监测系统统计数据 系统快照报告 1.运行时间 uptime命令会提供以下基本信息: 当前时间 系统运行的天数,小时数,分钟数 当前登录到系统的用户数 1分钟,5分钟 ...
- leetcode第一刷_Interleaving String
有关这样的字符串的题真是层出不穷啊,并且他们都有这样一个特点,就是递归的思路如此简单,但一定超时! 这个时候,dp就朝我们缓缓走来.递归超,dp搞!这道题的状态转移方程还是比較好写的,用ispart[ ...
- 2015移动安全挑战赛(阿里&看雪主办)第一题分析
今天在网上看到了阿里移动安全比赛的第一次,并且说难度不大,便拿来看了看. 主体就是找出一个密码输进去,然后看正误. 这个题是纯Java层的一个题,也没用进行什么保护.可以直接反编译. 登陆Button ...
- python 之路,Day11 (下)- sqlalchemy ORM
python 之路,Day11 - sqlalchemy ORM 本节内容 ORM介绍 sqlalchemy安装 sqlalchemy基本使用 多外键关联 多对多关系 表结构设计作业 1. ORM ...
- FineUI开发实践-目录
点我订阅 目前所有博客的截图,方便离线观看,点图片 FineUI初学手册 下载,实例项目搭建 FineUI初学手册-部分JS整理 部分JS整理 ASP.NET-FineUI开发实践-1 实际开发环境是 ...
- 【VB】StrConv函数 vbUnicode用法
[VB]StrConv函数 StrConv(string, conversion, LCID) vbUnicode 64 根据系统的缺省码页将字符串转成Unicode. vbFromUnicode 1 ...
- Mac下Sublime快捷键
由于自己笔记本是mac,造成window与mac中sublime快捷键不同,现在稍微整理下常用的方便于记忆: 1.control+alt+enter 打开Emmet(Zencoding) 2.supe ...
- OpenXmlSdk导出Excel
感觉OpenXmlSdk的语法真的不是很友好.研究了半天,只实现了简单的导出功能.对于单元格样式的设置暂时还是搞明白,网上的资料真的很少,官方文档是英文的.中文的文章大都是用工具(Open XML S ...