UVA10561 Treblecross 组合游戏/SG定理
Treblecross is a two player gamewhere the goal is to get three X in a row on a one-dimensional board. At the startof 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 beingthree X next to each other, that player wins.
Given the current state of the game, you are todetermine if the player to move can win the game assuming both players playperfectly. If so, you should also print all moves that will eventually lead toa win.
Consider the game where the board size is 5cells. If the first player puts a X at position three (in the middle) so thestate becomes ..X.., he will win the game as no matter where the other playerputs his X, the first player can get three X in a row. If, on the other hand,the first player puts the X in any other position, the second player will winthe game by putting the X in the opposite corner (for instance, after thesecond player moves the state might be .X..X). This will force the first playerto 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 ona line by itself. The string will only contain the characters '.' and 'X'. Thelength of the string (the size of the board) will be between 3 and 200characters, inclusive. No state will contain three X in a row.
Output
For each case, first output WINNING or LOSING depending onif the player to move will win or lose the game. On the next line, output inincreasing order all positions on the board where the player to move may put anX and win the game. The positions should be separated by a blank, and be inincreasing order. The leftmost position on the board is 1.
SampleInput Outputfor Sample Input
4 ..... X.....X..X.............X....X..X .X.X...X ............................................... |
WINNING 3 LOSING
WINNING 3 WINNING 1 12 15 17 20 24 28 31 33 36 47
|
题意:给定一个串,上面有'X'和'.',可以在'.'的位置放X,谁先放出3个'X'就赢了,求先手必胜的策略
题解:SG函数,每个串要是上面有一个X,周围的4个位置就是禁区了(放下去必败),所以可以以X分为几个子游戏去求SG函数的异或和进行判断,至于求策略,就是枚举每个位置就可以了
#include<bits/stdc++.h>
#define N 250
#define mes(x) memset(x, 0, sizeof(x));
#define ll __int64
const long long mod = 1e9+;
const int MAX = 0x7ffffff;
using namespace std;
int SG[N], dir[N], a[N], l;
char s[N];
int f(){
int num, i, ans;
for(ans=num=i=;i<=l;i++)
if(!a[i]) num++;
else{
ans ^= SG[num];
num = ;
}
return ans;
}
void getsg(){
int n, x, j;
SG[] = ;
SG[] = SG[] = SG[] = ;
for(n=;n<=;n++){
mes(dir);
for(x=n-;x<=n-;x++)
if(x>=) dir[SG[x]] = ;
for(x=;n--x>=;x++)
dir[SG[x]^SG[n--x]] = ;
for(j=;;j++){
if(!dir[j]){
SG[n] = j;
break;
}
}
}
}
int main()
{
int T, i, flag, j;
getsg();
scanf("%d", &T);
while(T--){
scanf("%s", s);
l = strlen(s);
if(strstr(s,"X.X")||strstr(s, "XX")){
mes(dir);
printf("WINNING\n");
for(i=;i<l-;i++){
if(s[i] == 'X'&&s[i+] == 'X') dir[i+] = ;
if(s[i] == 'X'&&s[i+] == 'X'){
dir[i+] = ;
if(i >= )
dir[i-] = ;
}
}
if(s[l-] == 'X'&& s[l-] == 'X')
dir[l-] = ;
for(flag=i=;i<;i++)
if(dir[i]){
printf("%s%d", flag?" ":"", i+);
flag = ;
}
printf("\n");
}
else{
mes(dir);
for(i=;i<l;i++)
if(s[i] == 'X')
for(j=i-;j<=i+;j++)
if(j<l&&j>=) dir[j] = ;
memcpy(a, dir, sizeof(a));
a[l] = ;
if(!f()){
printf("LOSING\n\n");
continue;
}
printf("WINNING\n");
for(flag=,i=;i<l;i++){
memcpy(a, dir, sizeof(a));
a[l] = ;
if(!dir[i]){
for(j=i-;j<=i+;j++)
if(j<l&&j>=) a[j] = ;
if(!f()){
printf("%s%d", flag?"":" ", i+);
flag = ;
}
}
}
printf("\n");
}
}
}
UVA10561 Treblecross 组合游戏/SG定理的更多相关文章
- 组合游戏 - SG函数和SG定理
在介绍SG函数和SG定理之前我们先介绍介绍必胜点与必败点吧. 必胜点和必败点的概念: P点:必败点,换而言之,就是谁处于此位置,则在双方操作正确的情况下必败. N点:必胜点 ...
- HDU 1536 S-Nim (组合游戏+SG函数)
题意:针对Nim博弈,给定上一个集合,然后下面有 m 个询问,每个询问有 x 堆石子 ,问你每次只能从某一个堆中取出 y 个石子,并且这个 y 必须属于给定的集合,问你先手胜还是负. 析:一个很简单的 ...
- hdu 3980 Paint Chain 组合游戏 SG函数
题目链接 题意 有一个\(n\)个珠子的环,两人轮流给环上的珠子涂色.规定每次涂色必须涂连续的\(m\)颗珠子,无法继续操作的人输.问先手能否赢. 思路 参考 转化 第一个人取完之后就变成了一条链,现 ...
- hdu 1848 Fibonacci again and again 组合游戏 SG函数
题目链接 题意 三堆石子,分别为\(m,n,p\)个,两人依次取石子,每次只能在一堆当中取,并且取的个数只能是斐波那契数.最后没石子可取的人为负.问先手会赢还是会输? 思路 直接按定义计算\(SG\) ...
- 博弈论题目总结(二)——SG组合游戏及变形
SG函数 为了更一般化博弈问题,我们引入SG函数 SG函数有如下性质: 1.如果某个状态SG函数值为0,则它后继的每个状态SG函数值都不为0 2.如果某个状态SG函数值不为0,则它至少存在一个后继的状 ...
- 【博弈论】组合游戏及SG函数浅析
目录 预备知识 普通的Nim游戏 SG函数 预备知识 公平组合游戏(ICG) 若一个游戏满足: 由两名玩家交替行动: 游戏中任意时刻,合法操作集合只取决于这个局面本身: 若轮到某位选手时,若该选手无合 ...
- UOJ 266 - 【清华集训2016】Alice和Bob又在玩游戏(SG 定理+01-trie)
题面传送门 神仙题. 首先注意到此题的游戏是一个 ICG,故考虑使用 SG 定理解决这个题,显然我们只需对每个连通块计算一遍其 SG 值异或起来检验是否非零即可.注意到我们每删除一个点到根节点的路径后 ...
- HDU 1851 (巴什博奕 SG定理) A Simple Game
这是由n个巴什博奕的游戏合成的组合游戏. 对于一个有m个石子,每次至多取l个的巴什博奕,这个状态的SG函数值为m % (l + 1). 然后根据SG定理,合成游戏的SG函数就是各个子游戏SG函数值的异 ...
- SG函数和SG定理【详解】
在介绍SG函数和SG定理之前我们先介绍介绍必胜点与必败点吧. 必胜点和必败点的概念: P点:必败点,换而言之,就是谁处于此位置,则在双方操作正确的情况下必败. N点:必胜点 ...
随机推荐
- 解决微信浏览器无法使用window.location.reload()刷新页面
解决方法: 使用 window.location.href=window.location.href+随机数 代替 window.location.reload(). function r ...
- php初学第一课
php:强大的脚本语言 B/S:brower:浏览器 server:服务器 C/S: client:客户端 server:服务器 一.php嵌入页面的标记 1. <?php ?> # ...
- Spinner的深入学习
简介: spinner是一个列表选择框,会在用户选择后,展示一个列表供用户进行选择.Spinner是ViewGroup的间接子类,它和其他的Android控件一样,数据需要使用Adapter进行封装. ...
- [转]RamDisk导致远程桌面客户端无法启动问题
在一次重启系统后发现无法运行远程桌面客户端,运行后进行连接即报错. 查看日志有AppCrash错误: 错误应用程序名称: mstsc.exe,版本: 6.1.7600.16385,时间戳: 0x4a5 ...
- 【iCore3 双核心板_FPGA】例程四:Tcl脚本实验——配置引脚
实验指导书及代码包下载: http://pan.baidu.com/s/1pJZDz0v iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...
- 【iCore3 双核心板_ uC/OS-III】例程六:信号量——共享资源
实验指导书及代码包下载: http://pan.baidu.com/s/1milKoVA iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...
- mysql集群 MySQL Cluster
<?php /* 郑重说明2015年6月11日16:28:14,目前为止MySQL Cluster 社区版不支持INNODB,商业版支持,但是授权价格20W左右,so看此文档之前,考虑下钱 My ...
- C语言输入多组问题~ungetc回退字符到stdin
题目描述 输入数组长度 n 输入数组 a[1...n] 输入查找个数m 输入查找数字b[1...m] 输出 YES or NO 查找有则YES 否则NO . 输入描述: 输入有多组数据 ...
- ArcGIS Server 增加缓存路径
Server缓存服务,由于缓存文件经常比较大,默认放在C盘下容易导致磁盘空间不够,因此Server提供了增加缓存路径的方法来解决该问题. 增加的路径有两种,一种是Server所在服务器增加一个和原缓存 ...
- C#7.0中有新特性
以下将是 C# 7.0 中所有计划的语言特性的描述.随着 Visual Studio “15” Preview 4 版本的发布,这些特性中的大部分将活跃起来.现在是时候来展示这些特性,你也告诉借此告诉 ...