HDU5724 Chess(SG定理)
题目
Source
http://acm.hdu.edu.cn/showproblem.php?pid=5724
Description
Alice and Bob are playing a special chess game on an n × 20 chessboard. There are several chesses on the chessboard. They can move one chess in one turn. If there are no other chesses on the right adjacent block of the moved chess, move the chess to its right adjacent block. Otherwise, skip over these chesses and move to the right adjacent block of them. Two chesses can’t be placed at one block and no chess can be placed out of the chessboard. When someone can’t move any chess during his/her turn, he/she will lose the game. Alice always take the first turn. Both Alice and Bob will play the game with the best strategy. Alice wants to know if she can win the game.
Input
Multiple test cases.
The first line contains an integer T(T≤100), indicates the number of test cases.
For each test case, the first line contains a single integer n(n≤1000), the number of lines of chessboard.
Then n lines, the first integer of ith line is m(m≤20), indicates the number of chesses on the ith line of the chessboard. Then m integers pj(1≤pj≤20) followed, the position of each chess.
Output
For each test case, output one line of “YES” if Alice can win the game, “NO” otherwise.
Sample Input
2
1
2 19 20
2
1 19
1 18
Sample Output
NO
YES
分析
题目大概说有n行,每行20格子,都有一些棋子,两个人轮流进行这个操作:选择某一行一个棋子移动到该行右边第一个空的格子。不能进行的人输。问先手是否能赢。
这个博弈显然是组合博弈,先手后手交替进行、每次决策是有限的、有胜利条件。。然后SG定理搞了。。求SG值要注意时间复杂度。
代码
#include<cstdio>
#include<cstring>
using namespace std;
int sg[1<<20];
int main(){
for(int s=0; s<(1<<20); ++s){
int k=100;
bool vis[21]={0};
for(int i=19; i>=0; --i){
if((s>>i&1)==0) continue;
if(k<=i-1){
vis[sg[(s^(1<<i))^(1<<k)]]=1;
}else{
for(int j=i-1; j>=0; --j){
if((s>>j&1)==0){
k=j;
vis[sg[(s^(1<<i))^(1<<k)]]=1;
break;
}
}
}
}
for(int i=0; i<=20; ++i){
if(!vis[i]){
sg[s]=i;
break;
}
}
} int t,n,m,a;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
int res=0;
for(int i=0; i<n; ++i){
scanf("%d",&m);
int s=0;
while(m--){
scanf("%d",&a);
s|=1<<20-a;
}
res^=sg[s];
}
if(res) puts("YES");
else puts("NO");
}
return 0;
}
HDU5724 Chess(SG定理)的更多相关文章
- SG函数和SG定理【详解】
		在介绍SG函数和SG定理之前我们先介绍介绍必胜点与必败点吧. 必胜点和必败点的概念: P点:必败点,换而言之,就是谁处于此位置,则在双方操作正确的情况下必败. N点:必胜点 ... 
- (转载)--SG函数和SG定理【详解】
		在介绍SG函数和SG定理之前我们先介绍介绍必胜点与必败点吧. 必胜点和必败点的概念: P点:必败点,换而言之,就是谁处于此位置,则在双方操作正确的情况下必败. N点:必胜点 ... 
- 组合游戏 - SG函数和SG定理
		在介绍SG函数和SG定理之前我们先介绍介绍必胜点与必败点吧. 必胜点和必败点的概念: P点:必败点,换而言之,就是谁处于此位置,则在双方操作正确的情况下必败. N点:必胜点 ... 
- HDU5795A Simple Nim SG定理
		A Simple Nim Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ... 
- HDU 1851 (巴什博奕 SG定理) A Simple Game
		这是由n个巴什博奕的游戏合成的组合游戏. 对于一个有m个石子,每次至多取l个的巴什博奕,这个状态的SG函数值为m % (l + 1). 然后根据SG定理,合成游戏的SG函数就是各个子游戏SG函数值的异 ... 
- 简单易懂的博弈论讲解(巴什博弈、尼姆博弈、威佐夫博弈、斐波那契博弈、SG定理)
		博弈论入门: 巴什博弈: 两个顶尖聪明的人在玩游戏,有一堆$n$个石子,每次每个人能取$[1,m]$个石子,不能拿的人输,请问先手与后手谁必败? 我们分类讨论一下这个问题: 当$n\le m$时,这时 ... 
- 【bzoj3576】[Hnoi2014]江南乐  博弈论+SG定理+数学
		题目描述 两人进行 $T$ 轮游戏,给定参数 $F$ ,每轮给出 $N$ 堆石子,先手和后手轮流选择石子数大于等于 $F$ 的一堆,将其分成任意(大于1)堆,使得这些堆中石子数最多的和最少的相差不超过 ... 
- SG函数&&SG定理
		必胜点和必败点的概念: P点:必败点,换而言之,就是谁处于此位置,则在双方操作正确的情况下必败. N点:必胜点,处于此情况下,双方操作均正确的情况下必胜. 必胜点和必败点的 ... 
- SG定理与SG函数
		一个蒟蒻来口胡$SG$函数与$SG$定理. 要是发现有不对之处望指教. 首先我们来了解一下$Nim$游戏. $Nim$游戏是公平组合游戏的一种,意思是当前可行操作仅依赖于当前局势. 而经典$Nim$游 ... 
随机推荐
- [Android Pro]   Android 4.1 使用 Accessibility实现免Root自动批量安装功能
			reference to : http://www.infoq.com/cn/articles/android-accessibility-installing?utm_campaign=info ... 
- CSDN-markdown编辑器
			欢迎使用Markdown编辑器写博客 本Markdown编辑器使用StackEdit修改而来,用它写博客,将会带来全新的体验哦: Markdown和扩展Markdown简洁的语法 代码块高亮 图片链接 ... 
- hdu 2159  FATE
			题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2159 思路:二维完全背包,状态转移方程为: f[j][l]=max(f[j][l],f[j-b[i]] ... 
- VCC、VDD、VSS、 VEE 和VPP的区别
			在电子电路中,常可以看到VCC.VDD和VSS三种不同的符号,它们有什么区别呢? 一.解释 VCC:C=circuit 表示电路的意思, 即接入电路的电压: VDD:D=device 表示器件的意思, ... 
- .net学习之CTS、CLS和CLR
			CLR:公共语言运行时,就是所有.net语言写的程序的公共运行时环境,比如C#.VB.Net等语言写的程序需要运行在CLR上,然后CLR解析执行操作系统的相关指令,CLR是.net程序运行在操作系统的 ... 
- Delphi线程的终止
			当线程对象的Execute()执行完毕,我们就认为此线程终止了.这时候,它会调用Delphi的一个标准例程EndThread(),这个例程再调用API函数ExitThread().由ExitThrea ... 
- Shell编程基础教程4--控制流结构
			4.控制流结构 4.1.控制结构 4.2.if then else语句 格式: if 条件1 //如果条件1为真 then 命令1 //那么,执行命令1 el ... 
- Solr入门之(1)前言与概述
			一.前言:为何选择Solr 由于搜索引擎功能在门户社区中对提高用户体验有着重在门户社区中涉及大量需要搜索引擎的功能需求,目前在实现搜索引擎的方案上有几种方案可供选择: 1. 基于Lucene自己进行封 ... 
- Oracle 11g新特性 -- 延迟段
			11gR2之前的版本中,当创建一张表时,会自动分配段空间,这样做有几个弊端: 1. 初始创建表时就需要分配空间,自然会占用一些时间,如果初始化多张表,这种影响就被放大. 2. 如果很多表开始的一段时间 ... 
- GitHub 使用教程图文详解(转)
			大纲: 一.前言 二.GitHub简介 三.注册GitHub账号 四.配置GitHub 五.使用GitHub 六.参与GitHub中其它开源项目 七.总结 注,GitHub官网:https://git ... 
