LightOJ 1229 Treblecross(SG函数打表 + 遍历)题解
题意:给你一串含“.”和“X”的字串,每次一个玩家可以把‘."变成“X”,谁先弄到三个XXX就赢。假如先手必赢,输出所有能必赢的第一步,否则输出0。
思路:显然如果一个X周围两格有X那么肯定能一步变成XXX,所以两个人都要避免在自己回合产生这种情况。如果一开始就存在上述情况,那么肯定是那一步。否则我遍历每一个空格看看能不能下这一步。满足我在这个空格变成“X”不会造成上述情况,然后算出nim和是否留给对手一个必败态。
设sg[x]表示长度为x的空格的sg函数,然后我遍历1~x位置变成“X”,那么空格键会被我分成两块(比如.....我在3位置下X,那么空格被我分成了左0右0两块,注意X旁边两块不能动)。
代码:
#include<set>
#include<map>
#include<stack>
#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
typedef long long ll;
const int maxn = + ;
const int seed = ;
const ll MOD = 1e9 + ;
const int INF = 0x3f3f3f3f;
using namespace std;
int sg[maxn], s[maxn], ans[maxn], pos;
char str[maxn];
void getSG(){
sg[] = ;
for(int i = ; i < maxn; i++){
memset(s, , sizeof(s));
for(int j = ; j <= i; j++){
int t = ;
if(i - j - >= ) t ^= sg[i - j - ];
if(j - >= ) t ^= sg[j - ];
s[t] = ;
}
for(int j = ; j < maxn; j++){
if(!s[j]){
sg[i] = j;
break;
}
}
}
}
bool check(){
ll ret = ;
int num = , len = strlen(str);
for(int i = ; i < len; i++){
if(str[i] == 'X'){
if((i >= && str[i - ] == 'X') || (i >= && str[i - ] == 'X') || (i + < len && str[i + ] == 'X') || (i + < len && str[i + ] == 'X')){
return false;
}
if(str[i - num - ] == 'X' && i - num - >= )
num -= ;
num -= ;
if(num >= ) ret ^= sg[num];
num = ;
}
else{
num++;
}
}
if(str[len - - num] == 'X' && len - - num >= )
num -= ;
if(num >= ) ret ^= sg[num];
return ret == ;
}
void solve(){
pos = ;
int len = strlen(str);
for(int i = ; i < len; i++){
if(str[i] == '.'){
str[i] = 'X';
if(i + < len && i - >= && str[i + ] == 'X' && str[i - ] == 'X'){
ans[pos++] = i + ;
}
else if(i + < len && i + < len && str[i + ] == 'X' && str[i + ] == 'X'){
ans[pos++] = i + ;
}
else if(i - >= && i - >= && str[i - ] == 'X' && str[i - ] == 'X'){
ans[pos++] = i + ;
}
else if(check()){
ans[pos++] = i + ;
}
str[i] = '.';
}
}
}
int main(){
int T, Case = ;
getSG();
scanf("%d", &T);
while(T--){
scanf("%s", str);
solve();
printf("Case %d:", Case++);
if(pos == ) printf(" 0\n");
else{
for(int i = ; i < pos; i++){
printf(" %d", ans[i]);
}
printf("\n");
}
}
return ;
}
LightOJ 1229 Treblecross(SG函数打表 + 遍历)题解的更多相关文章
- hdu 2147 SG函数打表(手写也可以) 找规律
kiki's game Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 40000/1000 K (Java/Others) Total ...
- UPCOJ9526(SG函数打表,nim游戏异或规则)
#include<bits/stdc++.h>using namespace std;int f[1007],SG[1007],S[1007];//f为可以选取的石头个数,SG为sg函数, ...
- bzoj 1228 [SDOI2009]E&D SG函数打表 找规律
题目链接 Description 桌子上有2n 堆石子,编号为1..2n.将第2k-1 堆与第2k 堆(1 ≤ k ≤ n)为同一组.第i堆的石子个数用一个正整数Si表示.一次分割操作指的是,从桌子上 ...
- hdu1848(sg函数打表)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1848 题意:中文题诶- 思路:直接sg函数打表就好了 代码: #include <iostrea ...
- 51 nod1067 Bash游戏 V2(sg函数打表)
1067 Bash游戏 V2 1.0 秒 131,072.0 KB 5 分 1级题 有一堆石子共有N个.A B两个人轮流拿,A先拿.每次只能拿1,3,4颗,拿到最后1颗石子的人获胜.假设A B都非 ...
- Educational Codeforces Round 68 (Rated for Div. 2)D(SG函数打表,找规律)
#include<bits/stdc++.h>using namespace std;int sg[1007];int main(){ int t; cin>>t; while ...
- Nowcoder 挑战赛23 B 游戏 ( NIM博弈、SG函数打表 )
题目链接 题意 : 中文题.点链接 分析 : 前置技能是 SG 函数.NIM博弈变形 每次可取石子是约数的情况下.那么就要打出 SG 函数 才可以去通过异或操作判断一个局面的胜负 打 SG 函数的时候 ...
- UVa 10561 Treblecross (SG函数)
题意:给定上一行字符串,其中只有 X 和 . 并且没有连续的三个 X,两个玩家要分别在 . 上放 X,如果出现三个连续的 X,则该玩家胜利,现在问你先手胜还是败,如果是胜则输出第一步可能的位置. 析: ...
- lightoj 1229 - Treblecross 博弈论
思路:SG函数 枚举先手的每一个位置是否有必胜. 1)如果出现了XXX则必胜: 2)如果出现了XX或X.X则必败: 3)否则计算后手的sg值和. 代码如下: #include<iostream& ...
随机推荐
- IO流(8)递归删除带文件的目录
递归删除带内容的目录 * * 目录给定:demo * * 分析: * A:封装目录 * B:获取该目录下的所有文件或者文件夹的File数组 * C:遍历该File数组,得到每一个File对象 * D: ...
- java执行Shell命令
java程序中要执行linux命令主要依赖2个类:Process和Runtime首先看一下Process类:ProcessBuilder.start() 和 Runtime.exec 方法创建一个本机 ...
- inter x86 emulator accelerator(HAXM installer) not compatible with windows
在SDK manager中遇到如下错误:这将导致AVD后期运行和启动方面的问题. 解决办法: 在如下的网址里面下载haxm-windows_v6_2_0这个文件的压缩包,自己手动安装即可.网站如下:点 ...
- orm之路由层
一.简单配置 1.参数 第一个参数是正则表达式(如果要精准匹配:‘^publish/$’),或者加斜杠('^publish/') 第二个参数是视图函数(不要加括号) urlpatterns = [ u ...
- POJ:Dungeon Master(三维bfs模板题)
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16748 Accepted: 6522 D ...
- python 多线程小练习
需求:有100个数据,启动5个线程,每个线程分20个数据,怎么把这20个数据分别传给每个线程. 1. 利用多线程实现 import threading nums = list(range(100)) ...
- EF6添加mysql的edmx实体时报错:无法生成模型:“System.Data.StrongTypingException: 表“TableDetails”中列“IsPrimaryKey”的值为 DBNull
EF6.1.3 ,使用mysql5.7的实体数据模型时,提示: 由于出现以下异常,无法生成模型:“System.Data.StrongTypingException: 表“TableDetails”中 ...
- location对象查询字符串参数
虽然location.search可以返回从问号到URL末尾的所有内容,但却没有办法逐个访问其中的每个查询字符串参数.为此,可以创建下面这样一个函数,用以解析查询字符串,然后返回包含所有参数的一个对象 ...
- Nature重磅:Hinton、LeCun、Bengio三巨头权威科普深度学习
http://wallstreetcn.com/node/248376 借助深度学习,多处理层组成的计算模型可通过多层抽象来学习数据表征( representations).这些方法显著推动了语音识别 ...
- AVPlayerLayer
AVPlayerLayer 最后一个图层类型是AVPlayerLayer.尽管它不是Core Animation框架的一部分(AV前缀看上去像),AVPlayerLayer是有别的框架(AVFound ...