博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UVA10561 Treblecross 组合游戏/SG定理
阅读量:4311 次
发布时间:2019-06-06

本文共 3882 字,大约阅读时间需要 12 分钟。

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
#define N 250#define mes(x) memset(x, 0, sizeof(x));#define ll __int64const long long mod = 1e9+7;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=0;i<=l;i++) if(!a[i]) num++; else{ ans ^= SG[num]; num = 0; } return ans;}void getsg(){ int n, x, j; SG[0] = 0; SG[1] = SG[2] = SG[3] = 1; for(n=4;n<=200;n++){ mes(dir); for(x=n-5;x<=n-3;x++) if(x>=0) dir[SG[x]] = 1; for(x=1;n-5-x>=0;x++) dir[SG[x]^SG[n-5-x]] = 1; for(j=0;;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=0;i
= 1) dir[i-1] = 1; } } if(s[l-1] == 'X'&& s[l-2] == 'X') dir[l-3] = 1; for(flag=i=0;i<200;i++) if(dir[i]){ printf("%s%d", flag?" ":"", i+1); flag = 1; } printf("\n"); } else{ mes(dir); for(i=0;i
=0) dir[j] = 1; memcpy(a, dir, sizeof(a)); a[l] = 1; if(!f()){ printf("LOSING\n\n"); continue; } printf("WINNING\n"); for(flag=1,i=0;i
=0) a[j] = 1; if(!f()){ printf("%s%d", flag?"":" ", i+1); flag = 0; } } } printf("\n"); } }}

 

转载于:https://www.cnblogs.com/Noevon/p/6241175.html

你可能感兴趣的文章
Laravel框架学习笔记之任务调度(定时任务)
查看>>
laravel 定时任务秒级执行
查看>>
浅析 Laravel 官方文档推荐的 Nginx 配置
查看>>
Swagger在Laravel项目中的使用
查看>>
Laravel 的生命周期
查看>>
CentOS Docker 安装
查看>>
Nginx
查看>>
Navicat远程连接云主机数据库
查看>>
Nginx配置文件nginx.conf中文详解(总结)
查看>>
Mysql出现Table 'performance_schema.session_status' doesn't exist
查看>>
MySQL innert join、left join、right join等理解
查看>>
vivado模块封装ip/edf
查看>>
sdc时序约束
查看>>
Xilinx Jtag Access/svf文件/BSCANE2
查看>>
NoC片上网络
查看>>
开源SoC整理
查看>>
【2020-3-21】Mac安装Homebrew慢,解决办法
查看>>
influxdb 命令行输出时间为 yyyy-MM-dd HH:mm:ss(年月日时分秒)的方法
查看>>
已知子网掩码,确定ip地址范围
查看>>
判断时间或者数字是否连续
查看>>