题意:给定上一行字符串,其中只有 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. centos更换yum源为aliyun源

    国外的yum源由于众所周知的GFW原因,有的被墙,有的很慢,阿里云依靠强大的技术优势建立了国内的开源镜像.阿里云Linux安装镜像源地址:http://mirrors.aliyun.com/ 第一步: ...

  2. ES6 Decorator 修饰器

    目的:  修改类的一种方法,修饰器是一个函数 编译: 安装 babel-plugin-transform-decortators-legacy .babelrd      plugins: [&quo ...

  3. 动态规划:压缩编码;WirelessRouters;

    转载请注明~ 如果有理解不到位或错误的情况,劳烦大神指正,一定感激不尽! 题目来源:CCF201612-4 压缩编码 题目的意思是: 1. 顺序给定文字中n个单词出现的频率(次数): 2. 对这n个单 ...

  4. Oracle性能优化3-sql优化一定要等价

    做sql优化的前提瞧见是sql等价 1.MAX MIN写法的分与合 drop table t purge; create table t as select * from dba_objects; a ...

  5. mybatis入门--#{}和${}的区别

    我们知道,在mybatis中,sql语句是需要我们自己写的.跟在普通的sql不一样的是,我们在使用mybatis框架的时候,使用的占位符不是 ? 而是 #{} 有时候还会出现这个符号 ${} 这些符号 ...

  6. 201621123008 《Java程序设计》第12周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 2. 面向系统综合设计-图书馆管理系统或购物车 使用流与文件改造你的图书馆管理系统或购物车. 2.1 简述如何 ...

  7. c++文件中引用C代码

    下面提供一个比较完整的示例程序,一共有四个文件:main.cpp.test.c.test.h.test.hpp main.cpp #include "test.hpp" int m ...

  8. Python之路(第六篇)Python全局变量与局部变量、函数多层嵌套、函数递归

    一.局部变量与全局变量 1.在子程序中定义的变量称为局部变量,在程序的一开始定义的变量称为全局变量.全局变量作用域是整个程序,局部变量作用域是定义该变量的子程序. 全局变量没有任何缩进,在任何位置都可 ...

  9. tomcat与jmeter

    jmeter无法提取出Tomcat之外的其他服务器的指标. 为了克服这一现状,研发了一个服务器代理,jmeter通过这个代理来获取性能数据. 代理使用的是sigar开源库,他是一个java通过部分和一 ...

  10. keybd_event使用方法

    Windows提供了一个模拟键盘API函数Keybd_event(),使用该函数可以相应的屏蔽键盘的动作.Keybd_event()函数能触发一个按键事件,也就是说会产生一个WM_KEYDOWN或WM ...