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连续出现. 判断先 ...
随机推荐
- Windows系统制作Ubuntu启动U盘(命令行)
背景 现今Ubuntu系统的使用越来越多,考虑到日常办公还是用Windows系统,但开发的需求常常要有Linux系统.因此将Linux系统安装到U盘不失为一种好的选择.在Windows系统上制作Ubu ...
- [leetcode]210. Course Schedule II课程表II
There are a total of n courses you have to take, labeled from 0 to n-1. Some courses may have prereq ...
- C# 创建WebService的简单示例
工具Visual Studio 2013 1.创建一个空的Web应用程序. 2.鼠标右击项目,添加->新建项 选择Web服务(ASMX),点击添加.一个简单的webservice就创建完成了.
- git log的用法
git log 查看 提交历史默认不用任何参数的话,git log 会按提交时间列出所有的更新,最近的更新排在最上面. git log --graph --pretty=format:'%Cred%h ...
- 【linux】下Apache无法启动(8080端口被占用)
Linux下8080端口被占用,apache无法启动. 打开终端输入netstat -lnp|grep 8080 发现竟然是tcp6 占用里,因此ipv6启用占用了端口. 1.打开/etc/sysct ...
- cgi fast-cgi php-fpm区别
php-cli 是php在系统执行的程序,直接执行php文件: cgi和fast-cgi的区别1.cgi和fast-cgi都是php解析协议,负责解析服务器分发过来的php动态文件:cgi程序就会去解 ...
- springmvc.xml,context.xml和web.xml
1:springmvc.xml配置要点 一般它主要配置Controller的组件扫描器和视图解析器 下为:springmvc.xml文件 <?xml version="1.0" ...
- first H5
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- C#读取Excel表中的数据时混合字段部分数据没有
解决同一列中“字符串”和“数字”两种格式同时存在,读取时,不能正确显示“字符串”格式的问题:set xlsconn=CreateObject("ADODB.Connection") ...
- do_something方法解析
/** * 运行任务 * @param $interval * @return bool */ static public function do_something($interval) { //是 ...