题意:给定上一行字符串,其中只有 X 和 . 并且没有连续的三个 X,两个玩家要分别在 . 上放 X,如果出现三个连续的 X,则该玩家胜利,现在问你先手胜还是败,如果是胜则输出第一步可能的位置。

析:首先,如果输入中出现了 XX 或者 X.X,那么先手必胜,这种可以先处理,然后考虑剩下的,首先每个玩家肯定不能放X-1,X-2,X+1,X+2的位置,其中 X 表示格子X中已经是 X 了,因为一放上,那么下一个玩家就一定能胜利,除非没有其他地方可以放了,那么游戏也就可以终止了,然后除了这些地方,那么剩下的都是可以随便放了,而且分成了好几段了,也就是每一个都是一个子游戏,这个题就是求子游戏的和,我们g(x) 表示连续的 x 个位置的对应的SG函数值,我们来推一下这个式子,g(x) = mex{ g(x-3), g(x-4), g(x-5), g(1) xor g(x-6), g(2) xor g(x-7) ... },我们来分别解释一下,如果不能理解的建议画上图来看看。g(x-3)表示把 X 放在格子的最左边,那么他右边的相邻的两个也不能放了,所以是 x-3,同理可以知道 g(x-4), g(x-5),再解释一下 g(1) xor g(x-6),首先 xor 是异或,这个意思就是已经把连续的 x 个位置已经分成两部分了,左边 1 个,右边 x-6 个,为什么是 x - 6,因为 x 左边两个,右边两个不能放,再加上左边那一个和本身的那一个。边界就是 g(0) = 0, g(1) = g(2) = g(3) = 1。然后就能求出 SG 函数,怎么求第一步的位置,枚举,所有的 . ,然后再求新的游戏和,如果是 0 ,那么就是可能的位置,否则就不是。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
//#define all 1,n,1
#define FOR(i,x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 200 + 10;
const int maxm = 100 + 2;
const LL mod = 100000000;
const int dr[] = {-1, 1, 0, 0, 1, 1, -1, -1};
const int dc[] = {0, 0, 1, -1, 1, -1, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} int g[maxn], cnt[maxn];
char s[maxn]; bool judge(){
for(int i = 0; i < n; ++i) if(s[i] == 'X'){
if(i + 1 < n && s[i+1] == 'X') return true;
else if(i + 2 < n && s[i+2] == 'X') return true;
}
for(int i = 0; i < n; ++i) if(s[i] == 'X'){
int j = i - 1, cnt = 2; while(j >= 0 && cnt-- && s[j] == '.') s[j--] = 'x';
j = i + 1, cnt = 2; while(j < n && cnt-- && s[j] == '.') s[j++] = 'x';
}
int ans = 0;
for(int i = 0; i < n; ++i) if(s[i] == '.'){
int cnt = 0; while(s[i+cnt] == '.') ++cnt;
i += cnt;
ans ^= g[cnt];
}
return ans;
} int main(){
g[0] = 0; g[1] = g[2] = g[3] = 1;
g[4] = 2; g[5] = 2;
for(int i = 6; i <= 200; ++i){
for(int j = 3; j < 6; ++j) cnt[g[i-j]] = i;
for(int j = 6; j <= i; ++j)
cnt[g[i-j]^g[j-5]] = i;
for(int j = 0; ; ++j)
if(cnt[j] != i){ g[i] = j; break; }
}
int T; cin >> T;
getchar();
while(T--){
fgets(s, maxn, stdin);
n = strlen(s);
set<int> sets;
for(int i = 0; i < n; ++i) if(s[i] == 'X'){
if(i + 1 < n && s[i+1] == 'X'){
if(i) sets.insert(i);
if(i+2 < n) sets.insert(i+3);
}
else if(i + 2 < n && s[i+2] == 'X') sets.insert(i+2);
}
if(sets.sz){
puts("WINNING");
for(auto it = sets.begin(); it != sets.end(); ++it)
if(it == sets.begin()) printf("%d", *it);
else printf(" %d", *it);
printf("\n");
continue;
} for(int i = 0; i < n; ++i) if(s[i] == 'X'){
int j = i - 1, cnt = 2; while(j >= 0 && cnt-- && s[j] == '.') s[j--] = 'x';
j = i + 1, cnt = 2; while(j < n && cnt-- && s[j] == '.') s[j++] = 'x';
}
int ans = 0;
for(int i = 0; i < n; ++i) if(s[i] == '.'){
int cnt = 0; while(s[i+cnt] == '.') ++cnt;
i += cnt;
ans ^= g[cnt];
}
if(ans == 0){ puts("LOSING\n"); continue; }
puts("WINNING");
for(int i = 0; i < n; ++i) if(s[i] == 'x') s[i] = '.';
for(int i = 0, cnt = 0; i < n; ++i) if(s[i] == '.'){
s[i] = 'X';
if(!judge()){
if(cnt) putchar(' ');
printf("%d", i+1);
++cnt;
}
for(int j = 0; j < n; ++j) if(s[j] == 'x') s[j] = '.';
s[i] = '.';
}
putchar('\n');
}
return 0;
}

  

UVa 10561 Treblecross (SG函数)的更多相关文章

  1. UVA 10561 - Treblecross(博弈SG函数)

    UVA 10561 - Treblecross 题目链接 题意:给定一个串,上面有'X'和'.',能够在'.'的位置放X.谁先放出3个'X'就赢了,求先手必胜的策略 思路:SG函数,每一个串要是上面有 ...

  2. UVA 10561 Treblecross(博弈论)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=32209 [思路] 博弈论. 根据X分布划分禁区,每个可以放置的块为 ...

  3. UVa 10561 - Treblecross

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  4. UVA 11534 - Say Goodbye to Tic-Tac-Toe(博弈sg函数)

    UVA 11534 - Say Goodbye to Tic-Tac-Toe 题目链接 题意:给定一个序列,轮流放XO,要求不能有连续的XX或OO.最后一个放的人赢.问谁赢 思路:sg函数.每一段.. ...

  5. uva 10561 sg定理

    Problem C Treblecross Input: Standard Input Output: Standard Output Time Limit: 4 Seconds Treblecros ...

  6. UVA 11927 - Games Are Important(sg函数)

    UVA 11927 - Games Are Important option=com_onlinejudge&Itemid=8&page=show_problem&catego ...

  7. LightOJ 1229 Treblecross(SG函数打表 + 遍历)题解

    题意:给你一串含“.”和“X”的字串,每次一个玩家可以把‘."变成“X”,谁先弄到三个XXX就赢.假如先手必赢,输出所有能必赢的第一步,否则输出0. 思路:显然如果一个X周围两格有X那么肯定 ...

  8. Treblecross(uva 10561)

    题意:一个 1 × n 的棋盘,有 X 和 :,当棋盘上出现三个连续的X 时游戏结束,两人轮流操作,每次能把一个 : 变成 X,问先手必胜方案数以及先手可以放的位置. /* 对于先手,当有一个'X'时 ...

  9. 【UVA10561】Treblecross(SG函数)

    题意:有n个格子排成一行,其中一些格子里面有字符X.两个游戏者轮流操作,每次可以选一个空格,在里面放上字符X. 如果此时有3个连续的X出现,则该游戏者赢得比赛.初始条件下不会有3个X连续出现. 判断先 ...

随机推荐

  1. NET Runtime version 2.0.50727.42 - 执行引擎错误 或者无法创建应用程序域

    server2003操作系统 IIS运行应用程序报错,应用程序事件查看器详细: NET Runtime version 2.0.50727.42 - 执行引擎错误 或者无法创建应用程序域 解决方法:卸 ...

  2. java_15 System类

    1.System类 2.System类方法 (1)currentTimeMillis() public static void main(String[] args) { long start = S ...

  3. 关于MYSQL字符集问题(二)

    1.查看默认字符集(默认情况下,mysql的字符集是latin1(ISO_8859_1) 通常,查看系统的字符集和排序方式的设定可以通过下面的两条命令: mysql> SHOW VARIABLE ...

  4. Android的事件分发机制

    public boolean dispatchTouchEvent(MotionEvent event) 通过方法名我们不难猜测,它就是事件分发的重要方法.那么很明显,如果一个MotionEvent传 ...

  5. 一键查看IE密码!IE PassView简易教程

    IE浏览器保存在本地的各网站登录账号密码怎么查看?如果你想一下子就找到所有保存在IE的账号和密码,那IE PassView(IE密码查看器)能帮到你.IE PassView(IE密码查看器)非常小巧, ...

  6. Linux下Python2升级Python3

    Linux下Python2的升级方法: 一.下载Python3安装包: 1.在线下载 wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2 ...

  7. docker-ce-17.09 容器创建,运行,进入,删除,导入/导出

    docker容器是镜像运行的一个运行实例,带有额外的可写文件层. 一.创建容器 > docker create -it centos:latest create命令新建的容器处于停止状态,可以使 ...

  8. 我的开发小tip

    开发原则:1.谁开发,谁负责到底.自己的开发的模块自己维护,不要让别人替你维护,否则很麻烦:2.合理分配时间3.谨慎的处理遇到的bug和问题,不是自己开发的不要轻举妄动,提交到待办中即可4.万勿过度设 ...

  9. Lucene/Solr企业级搜索学习资源

    Solr是一个独立的企业级搜索应用服务器,它对外提供类似于Web-service的API接口.用户可以通过http请求,向搜索引擎服务器提交一定格式的XML文件,生成索引:也可以通过Http GSol ...

  10. 安装scrapy 出现error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools 错误

    安装scrapy 出现以下 错误: error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C ...