UVa 10561 Treblecross (SG函数)
题意:给定上一行字符串,其中只有 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函数)的更多相关文章
- UVA 10561 - Treblecross(博弈SG函数)
UVA 10561 - Treblecross 题目链接 题意:给定一个串,上面有'X'和'.',能够在'.'的位置放X.谁先放出3个'X'就赢了,求先手必胜的策略 思路:SG函数,每一个串要是上面有 ...
- UVA 10561 Treblecross(博弈论)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=32209 [思路] 博弈论. 根据X分布划分禁区,每个可以放置的块为 ...
- UVa 10561 - Treblecross
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- UVA 11534 - Say Goodbye to Tic-Tac-Toe(博弈sg函数)
UVA 11534 - Say Goodbye to Tic-Tac-Toe 题目链接 题意:给定一个序列,轮流放XO,要求不能有连续的XX或OO.最后一个放的人赢.问谁赢 思路:sg函数.每一段.. ...
- uva 10561 sg定理
Problem C Treblecross Input: Standard Input Output: Standard Output Time Limit: 4 Seconds Treblecros ...
- UVA 11927 - Games Are Important(sg函数)
UVA 11927 - Games Are Important option=com_onlinejudge&Itemid=8&page=show_problem&catego ...
- LightOJ 1229 Treblecross(SG函数打表 + 遍历)题解
题意:给你一串含“.”和“X”的字串,每次一个玩家可以把‘."变成“X”,谁先弄到三个XXX就赢.假如先手必赢,输出所有能必赢的第一步,否则输出0. 思路:显然如果一个X周围两格有X那么肯定 ...
- Treblecross(uva 10561)
题意:一个 1 × n 的棋盘,有 X 和 :,当棋盘上出现三个连续的X 时游戏结束,两人轮流操作,每次能把一个 : 变成 X,问先手必胜方案数以及先手可以放的位置. /* 对于先手,当有一个'X'时 ...
- 【UVA10561】Treblecross(SG函数)
题意:有n个格子排成一行,其中一些格子里面有字符X.两个游戏者轮流操作,每次可以选一个空格,在里面放上字符X. 如果此时有3个连续的X出现,则该游戏者赢得比赛.初始条件下不会有3个X连续出现. 判断先 ...
随机推荐
- LAB3 整数相加
//yuec2 Yue Cheng package lab3; public class Fraction { int numerator; int denominator; //obeject wi ...
- VS中,添加完Web引用(WebServer引用/Web服务引用),写代码时引用不到
VS中,添加完Web引用(WebServer引用/Web服务引用),写代码时引用不到 添加完之后要等一会儿 等一会儿 等一会儿 就有了
- html 导出pdf
地址: https://developers.itextpdf.com/examples/xml-worker/html-lists 主方法: public string Generate(strin ...
- Linux日志文件分割
经常有这样的情况,日志文件或者数据文件会变得很庞大,那么每次对这些目标文件的访问或者修改就会变得极其耗时.因而引发一些性能问题,但是如果对这些目标文件进行分割,限定其大小,那么每次的访问或者修改就会变 ...
- 进程同步(multiprocess.Lock、multiprocess.Semaphore、multiprocess.Event) day38
进程同步(multiprocess.Lock.multiprocess.Semaphore.multiprocess.Event) 锁 —— multiprocess.Lock 通过刚刚的学习,我们千 ...
- Redis进阶实践之二十 Redis的配置文件使用详解
一.引言 写完上一篇有关redis使用lua脚本的文章,就有意结束Redis这个系列的文章了,当然了,这里的结束只是我这个系列的结束,但是要学的东西还有很多.但是,好多天过去了,总是感觉好像还缺点什么 ...
- 在电脑上查看小米手机连接wifi时保存的密码
手机连接的wifi,想另一个手机也连上,密码忘了. 先备份手机的资料,然后将手机连上电脑,在SD卡上MIUI-backup-allbackup文件夹中, 找到刚才备份的文件夹 点进去找到WLAN设置. ...
- PAT 1018 锤子剪刀布(20)
1018 锤子剪刀布 (20)(20 分) 大家应该都会玩"锤子剪刀布"的游戏:两人同时给出手势,胜负规则如图所示: 现给出两人的交锋记录,请统计双方的胜.平.负次数,并且给出双方 ...
- 多字节字符集与Unicode字符集
在计算机中字符通常并不是保存为图像,每个字符都是使用一个编码来表示的,而每个字符究竟使用哪个编码代表,要取决于使用哪个字符集(charset). 多字节字符集: 在最初的时候,Internet上只有一 ...
- MySQL 系列(一)安装
MySQL 系列(一)安装 以 Centos7 下安装 MySQL 5.6 为例. 一.环境准备 (1) 下载 下载地址: https://dev.mysql.com/get/Downloads/My ...