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点:必胜点 ...
随机推荐
- BizTalk开发系列(十二) Schema设计之Group与Order
开发BizTalk项目的时候会先约定各系统之间往来的消息格式. 由于BizTalk内部唯一使用XML文档.因此消息的格式为XML Schema(XML Schema 用于描述 XML 文档的结构).虽 ...
- IOS第二天多线程-02一次性代码
********** #import "HMViewController.h" #import "HMImageDownloader.h" @interface ...
- poj2115-C Looooops(扩展欧几里德算法)
本题和poj1061青蛙问题同属一类,都运用到扩展欧几里德算法,可以参考poj1061,解题思路步骤基本都一样.一,题意: 对于for(i=A ; i!=B ;i+=C)循环语句,问在k位存储系统中循 ...
- String-自定义功能
<script> /* *发现js中的String对象有限,想要对字符串操作的其他功能. *比如:去除字符串两端的空格.这时只能自.定义 */ //去除字符串两端的空格 function ...
- 【iCore3 双核心板_ uC/OS-III】例程七:信号量——任务同步
实验指导书及代码包下载: http://pan.baidu.com/s/1kVjeN2n iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...
- 最实用的IT类网站及工具大集合[转]
1.聚合数据 大家在开发过程中,可能会用到各种各样的数据,想找一些接口来提供一些数据.比如天气预报查询,火车时刻表查询,彩票查询,身份证查询等等.有了这个接口,直接调用即可.各种各样的API接口满足你 ...
- A trip through the Graphics Pipeline 2011_12 Tessellation
Welcome back! This time, we’ll look into what is perhaps the “poster boy” feature introduced with th ...
- NEC学习 ---- 模块 -简易文字链接列表
为了方便查看, 在最外面设置了一个粉色框. html代码: <div class="container"> <ul class="m-list" ...
- 为什么微信android图片质量会比iphone的差?
为什么微信android图片质量会比iphone的差? 我们团队最初也纠结过这个问题,费了半天劲.绕了好大圈,直到最后才发现,原来这是谷歌犯得一个“小”错误,而且一直错到了今天. 谷歌的错就在于:li ...
- 基于netty的心跳机制实现
前言:在实现过程查找过许多资料,各种波折,最后综合多篇文章最终实现并上线使用.为了减少大家踩坑的时间,所以写了本文,希望有用.对于实现过程中有用的参考资料直接放上链接,可能有些内容相对冗余,不过时间允 ...