题目

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定理)的更多相关文章

  1. SG函数和SG定理【详解】

    在介绍SG函数和SG定理之前我们先介绍介绍必胜点与必败点吧. 必胜点和必败点的概念:        P点:必败点,换而言之,就是谁处于此位置,则在双方操作正确的情况下必败.        N点:必胜点 ...

  2. (转载)--SG函数和SG定理【详解】

    在介绍SG函数和SG定理之前我们先介绍介绍必胜点与必败点吧. 必胜点和必败点的概念:        P点:必败点,换而言之,就是谁处于此位置,则在双方操作正确的情况下必败.        N点:必胜点 ...

  3. 组合游戏 - SG函数和SG定理

    在介绍SG函数和SG定理之前我们先介绍介绍必胜点与必败点吧. 必胜点和必败点的概念:        P点:必败点,换而言之,就是谁处于此位置,则在双方操作正确的情况下必败.        N点:必胜点 ...

  4. HDU5795A Simple Nim SG定理

    A Simple Nim Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  5. HDU 1851 (巴什博奕 SG定理) A Simple Game

    这是由n个巴什博奕的游戏合成的组合游戏. 对于一个有m个石子,每次至多取l个的巴什博奕,这个状态的SG函数值为m % (l + 1). 然后根据SG定理,合成游戏的SG函数就是各个子游戏SG函数值的异 ...

  6. 简单易懂的博弈论讲解(巴什博弈、尼姆博弈、威佐夫博弈、斐波那契博弈、SG定理)

    博弈论入门: 巴什博弈: 两个顶尖聪明的人在玩游戏,有一堆$n$个石子,每次每个人能取$[1,m]$个石子,不能拿的人输,请问先手与后手谁必败? 我们分类讨论一下这个问题: 当$n\le m$时,这时 ...

  7. 【bzoj3576】[Hnoi2014]江南乐 博弈论+SG定理+数学

    题目描述 两人进行 $T$ 轮游戏,给定参数 $F$ ,每轮给出 $N$ 堆石子,先手和后手轮流选择石子数大于等于 $F$ 的一堆,将其分成任意(大于1)堆,使得这些堆中石子数最多的和最少的相差不超过 ...

  8. SG函数&&SG定理

    必胜点和必败点的概念:        P点:必败点,换而言之,就是谁处于此位置,则在双方操作正确的情况下必败.        N点:必胜点,处于此情况下,双方操作均正确的情况下必胜. 必胜点和必败点的 ...

  9. SG定理与SG函数

    一个蒟蒻来口胡$SG$函数与$SG$定理. 要是发现有不对之处望指教. 首先我们来了解一下$Nim$游戏. $Nim$游戏是公平组合游戏的一种,意思是当前可行操作仅依赖于当前局势. 而经典$Nim$游 ...

随机推荐

  1. ios wax热更新之安装wax(xcode7.3.1)

    通过Finder浏览到你保存该项目的文件夹.创建三个新的文件夹:wax.scripts和Classes. 第一:首先,下载源代码的压缩包.Wax放在GitHub上(https://github.com ...

  2. 网页(HTML)中的特殊字符

    网页(HTML)中的特殊字符 (1)一般来说,在HTML中,一个特殊字符有两种表达方式,一种称作数字参考,一种称作实体参考. 所谓数字参考,就是用数字来表示文档中的特殊字符,通常由前缀“&#” ...

  3. iOS 判断第一个字符是数字还是汉字

       NSString *titleStr = @"琳小兮";  //先截取字符串,拿到第一个字符         NSString *firstStr = [titleStr s ...

  4. Android -- android:configChanges

    原文:http://jingyan.baidu.com/article/2fb0ba4056b25700f2ec5faf.html 从Android 3.2(API 13),如果你想阻止程序在运行时重 ...

  5. WPF框架MVVM简单例子

    MVVM是Model-View-ViewModel的缩写形式,它通常被用于WPF或Silverlight开发.Model——可以理解为带有字段,属性的类.View——可以理解为我们所看到的UI.Vie ...

  6. ytu 1061: 从三个数中找出最大的数(水题,模板函数练习 + 宏定义练习)

    1061: 从三个数中找出最大的数 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 154  Solved: 124[Submit][Status][We ...

  7. SQL Server 2014 BI新特性(二)结合Data Explorer和GeoFlow进行数据分析

    Data Explorer和GeoFlow作为Excel的新功能被写入到即将发布的SQL Server 2014当中.Data Explorer为业务分析人员提供了一种数据获取,整理以及组织的方式,通 ...

  8. C++读取txt文件

    1. 逐行读入 void readTxt(string file) { ifstream infile; infile.open(file.data()); //将文件流对象与文件连接起来 asser ...

  9. SercureCRT无法正常连接Ubuntu14.0.4.1的解决办法

    问题描述 通过VirtualBox重新安装了ubuntu 14.0.4.1 虚拟服务器,在SercureCRT中使用root帐号连接ubuntu14.0.4.1的时候,提示“Password Auth ...

  10. java线程之——sleep()与wait()的区别

    sleep()是Thread的方法,wait()是Object的方法 如果线程进入了同步锁,sleep不会释放对象锁,wait会释放对象锁 sleep的作用就是让正在执行的线程主动让出CPU,给其它线 ...