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$游 ...
随机推荐
- July 1st, Week 27th Friday, 2016
It does not do to dwell on dreams, and forget to live. 不要生活在梦里,不要沉醉于空想而疏忽了生活. Stand straightly, and ...
- centOS目录结构
rpm包到 /usr/bin/dirName 二进制包(需要解压 -> 生成配置文件 -> make(编译) -> 安装)放到 /usr/local/src/** mongodb ...
- 利用 Rational ClearCase ClearMake 构建高性能的企业级构建环境
转载地址:http://www.ibm.com/developerworks/cn/rational/r-cn-clearmakebuild/ 构建管理是 IBM® Rational® ClearCa ...
- iOS系类教程之用instruments来检验你的app
比较了好多关于instruments 还是发现老外写的比较牛逼.于是果断翻译过来.有能力的的可以去看英文原版,鼓励大家看原版资料远离二手教程.这里是原文 入门 为了节省大家的时间,提供一个演示的D ...
- iOS开发--UIDatePicker
UIDatePicker 是一个控制器类,封装了 UIPickerView,但是他是UIControl的子类,专门用于接受日期.时间和持续时长的输入.日期选取器的各列会按照指定的风格进行自动配置,这样 ...
- CLR via C#(09)-扩展方法
对于一些现成的类,如果我们想添加一些新的方法来完善功能,但是不想改变已有的封装,也不想使用派生类,那么该怎么办呢?这里我们可以使用扩展方法. 一见钟情--初识扩展 扩展方法使您能够向现有类型“添加”方 ...
- Genymotion刷入谷歌应用市场以及获取root权限
Genymotion刷入谷歌应用市场以及获取root权限 - 推酷http://www.tuicool.com/articles/rEV3aa6 刷入gapp, arm框架,supersu的包要注意, ...
- Delphi面向对象---接口
从Delphi3开始支持接口.接口定义了能够与一个对象进行交互操作的一组过程和函数.对一个接口进行定义包含两个方面的内容: 1)一方面是实现这个接口 2)另一方面是定义接口的客户 一个类能够实现多个接 ...
- Server.MapPath查询路径那几件事
主要总结Server.MapPath 这个方法的使用以及使用的场景,不是什么时候都适合使用: 1.实现功能: Server.MapPath能够获取指定URL相对服务器的物理路径,在IIS服务端,能够根 ...
- Microsoft SQL Server 博客目录
基础概念篇 SQL Server排序规则 SQL SERVER 统计信息概述(Statistics) SQL SERVER 索引之聚集索引和非聚集索引的描述 Sql Server 索引之唯一索引和筛选 ...