UVa 10561 (SG函数 递推) Treblecross
如果已经有三个相邻的X,则先手已经输了。
如果有两个相邻的X或者两个X相隔一个.,那么先手一定胜。
除去上面两种情况,每个X周围两个格子不能再放X了,因为放完之后,对手下一轮再放一个就输了。
最后当“禁区”布满整行,不能再放X了,那个人就输了。
每放一个X,禁区会把它所在的线段“分割”开来,这若干个片段就可以看做若干个游戏的和。
设g(x)表示x个连续格子对应的SG函数值,递推来求g(x):
g(x) = mex{ g(x-3), g(x-4), g(x-5), g(x-6) xor g(1), g(x-7) xor g(2)... }
g(0) = 0, g(1) = g(2) = g(3) = 1
枚举策略时,就是模拟下一步的状态,记录其中所有必败状态。
#include <cstdio>
#include <cstring> const int maxn = ;
char s[maxn + ];
int g[maxn + ], ans[maxn + ];
bool vis[maxn + ]; bool winning(const char* s)
{
int n = strlen(s);
for(int i = ; i < n-; i++)//已经有三个相邻的X,先手输
if(s[i] == 'X' && s[i+] == 'X' && s[i+] == 'X') return false; bool no[n+];
memset(no, false, sizeof(no));
for(int i = ; i < n; i++) if(s[i] == 'X')
{
for(int d = -; d <= ; d++)
{
if(i+d >= && i+d < n)
{
if(d != && s[i+d] == 'X') return true;//有两个X在彼此的禁区,先手胜
no[i+d] = true;//设置禁区
}
}
} no[n] = ;
int sg = ;
for(int i = ; i < n; i++)
{
if(no[i]) continue;
int cnt = ;
while(i < n && !no[i]) { i++; cnt++; }
sg ^= g[cnt];
}
return sg != ;
} int main()
{
//freopen("in.txt", "r", stdin); g[] = ;
g[] = g[] = g[] = ;
for(int i = ; i <= maxn; i++)
{//递推求函数g
memset(vis, false, sizeof(vis));
for(int j = ; i-j >= ; j++)
{
int v = ;
v ^= g[i-j];
int x = j - ;
if(x > ) v ^= g[x];
vis[v] = true; for(int j = ; ; j++) if(!vis[j]) { g[i] = j; break; }
}
} int T;
scanf("%d", &T);
while(T--)
{
scanf("%s", s);
if(!winning(s)) { printf("LOSING\n\n"); continue; } puts("WINNING");
int n = strlen(s);
memset(ans, , sizeof(ans));
int p = ;
for(int i = ; i < n; i++) if(s[i] == '.')
{
s[i] = 'X';
if(!winning(s)) ans[p++] = i+;//后继必败状态便是先手下一步的策略
s[i] = '.';
}
for(int i = ; i < p; i++)
{
if(i) printf(" ");
printf("%d", ans[i]);
}
printf("\n");
} return ;
}
代码君
UVa 10561 (SG函数 递推) Treblecross的更多相关文章
- 一类SG函数递推性质的深入分析——2018ACM陕西邀请赛H题
题目描述 定义一种有根二叉树\(T(n)\)如下: (1)\(T(1)\)是一条长度为\(p\)的链: (2)\(T(2)\)是一条长度为\(q\)的链: (3)\(T(i)\)是一棵二叉树,它的左子 ...
- Light OJ 1296 - Again Stone Game (博弈sg函数递推)
F - Again Stone Game Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu ...
- 【主席树维护mex】 【SG函数递推】 Problem H. Cups and Beans 2017.8.11
Problem H. Cups and Beans 2017.8.11 原题: There are N cups numbered 0 through N − 1. For each i(1 ≤ i ...
- UVa 12034 - Race(递推 + 杨辉三角)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- POJ_3090 Visible Lattice Points 【欧拉函数 + 递推】
一.题目 A lattice point (x, y) in the first quadrant (x and y are integers greater than or equal to 0), ...
- uva 10561 sg定理
Problem C Treblecross Input: Standard Input Output: Standard Output Time Limit: 4 Seconds Treblecros ...
- UVA 10288 - Coupons(概率递推)
UVA 10288 - Coupons option=com_onlinejudge&Itemid=8&page=show_problem&category=482&p ...
- uva 11375 Matches (递推)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- UVA 557 - Burger(概率 递推)
Burger When Mr. and Mrs. Clinton's twin sons Ben and Bill had their tenth birthday, the party was ...
随机推荐
- cadence16.6 中orcad导出网表时ERROR (ORCAP-5004)
ORCAD网表输出时 ERROR (ORCAP-5004):Error initializing COM property pages 之前遇到过这个问题,解决后忘了记录下来了.依稀记得问题答 ...
- linux消息队列的使用
消息队列 *消息队列是内核地址空间中的内部链表,通过内核在各个进程之间传递的内容.消息顺序发送到消息队列中,每个消息队列都有IPC标识符唯一地进行标识. msgbuf结构 struct msgbuf{ ...
- FZU 2016 summer train I. Approximating a Constant Range 单调队列
题目链接: 题目 I. Approximating a Constant Range time limit per test:2 seconds memory limit per test:256 m ...
- CSS基础知识学习笔记
1.css基本样式讲解 1.1 css背景background-attachment:背景图像是否固定或者随着页面的其余部分滚动background-color:设置元素的背景颜色background ...
- [转载]C#如何在webBrowser1控件通过TagName,Name查找元素(没有ID时)遍历窗体元素
//防止页面多次刷新页面执行 ) { string GetUserName = System.Configuration.ConfigurationSettings.AppSettings[" ...
- OpenLDAP配置信息记录
随着各种研发工具使用越来越多,单独为每个工具维护一个账号系统的开销越来越大,而且作为用户多个账号密码使用也越来越不方便.所以需要做一个统一账号登陆. 查询了多个方法,又因为之前用过LDAP,所以选择了 ...
- 数据库批量插入数据的shell脚本
测试用,先来一个简单的,这个是国产神通数据库的,用isql命令: !/bin/bash == "-h" ] then echo "USAGE: $0 table_name ...
- Comparable & Comparator
Comparable & Comparator 都是用来实现集合中元素的比较.排序的,只是 Comparable 是在集合内部定义的方法实现的排序,Comparator 是在集合外部实现的排序 ...
- crontab定时运行git命令 更新代码库
Q: http://stackoverflow.com/questions/7994663/git-push-via-cron I'm trying to run a git push fro ...
- Android Service 的一些笔记
绑定服务: 用于间接调用服务里面的方法.如果调用者Activity被销毁了,服务也跟着销毁了,服务也会跟着销毁. 开启服务: 不可以调用服务里面的方法.如果调用者的Activity退出了,服务还会长期 ...