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$游 ...
随机推荐
- Linux定时任务设定
使用crontab 命令进行设定. 详情可参见:http://blog.csdn.net/xiyuan1999/article/details/8160977. 共有6项构成,前5项为时间:分 时 天 ...
- Navicat 回复 psc 文件 Mysql
在mysql 中回复 psc文件 的时候 只能一步步来,先在navicat中建一个空数据库,然后点击有上角的备份==>回复备份==> 找到psc文件==> 注意此时不要急于点击 开始 ...
- 浅谈 switch和if
1.所有的switch 都可以用if 替换,但所有的if不一定能被switch替换 2.:switch case直接跳到对应的case值里面执行相应代码.而if语句会执行一条一条判断语句,直到匹配到对 ...
- 让ASP.NET MVC不使用jsonp也可以跨域访问
跨域问题仅仅发生在Javascript发起AJAX调用,或者Silverlight发起服务调用时,其根本原因是因为浏览器对于这两种请求,所给予的权限是较低的,通常只允许调用本域中的资源,除非目标服务器 ...
- Delphi字符串的基本操作与常用函数
参考:http://www.cnblogs.com/pchmonster/archive/2011/12/16/2290034.html 结合这个博客一起学习:http://www.cnblogs.c ...
- 【Java EE 学习 21 上】【其它类型的监听器】【使用HttpSessionActivationListener监听session的活化和钝化】
一.ServletContextListener Method Summary void contextDestroyed(ServletContextEvent sce) R ...
- 【PHP&&FileIO】
在程序员的眼中,文件不应当仅仅是一部电影.一首歌曲.一个pdf文件,它应该被视为一个文件夹,而我们所熟知的文件,应当是它的特例. 在web开发中,文件的上传和下载是文件变成的一个实际应用. 延续cru ...
- Message Flood
Message Flood Time Limit: 1500MS Memory limit: 65536K 题目描述 Well, how do you feel about mobile phone? ...
- SQL集合操作符样例(UNION,INTERSECT,EXCEPT)
由于MYSQL目前只实现了UNION, 所以后面两个就没得玩罗.... SELECT emp_id, assigned_branch_id -> FROM employee -> WHER ...
- Liunx-https-java.lang.NoClassDefFoundError: javax/crypto/SunJCE_b
错误信息: java.lang.NoClassDefFoundError: javax/crypto/SunJCE_b at javax.crypto.KeyGenerator.a(DashoA13* ...