HDU 5724 Chess国际象棋

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

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.

Alice和Bob正在一个n × 20的棋盘上玩自定国际象棋。

棋盘上有若干子。每方在本回合只能行一子。如果被移动的子的右邻格为空,则移动之。否则跳过挡路的子,直接移动到他们的右边。一格只能落一子,并且棋子不能移出界。若某人在己方回合无法移动任何子,则输。

Alice总是执先手。Alice与Bob都会使用最佳策略。Alice希望得知自己是否能赢得这场游戏。

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.

多组测试用例。

第一行有一个整数T(T≤100),表示测试用例的数量。

对于每个测试用例,第一行有一个整数n(n≤1000),表示棋子的行数。

随后n行,每行第一个数为m(m≤20),表示此行棋子的数量。接着有m个整数pj(1≤pj≤20),表示各个棋子的位置。

Output

输出

For each test case, output one line of “YES” if Alice can win the game, “NO” otherwise.

对于每个测试用例,如果Alice能赢则输出“YES”,否则输出“NO”。

Sample Input - 输入样例

Sample Output - 输出样例

2
1
2 19 20
2
1 19
1 18

NO
YES

【题解】

SG定理,百度上解释看着不明觉厉,自己琢磨实现拙劣难成,最后再结合别人的代码似乎自行理解了一下,然而回头看百度上的解释依然看不懂。

总结自百度:

这种游戏必定能分出输赢,不是我方输就是对方赢。每种状态都当初是我发先手,最后必输的状态是确定的。即SG[x] = 0,必输。

如果我方的起手状态是SG[x] = 0的上一个状态,则必定 SG[y] = 1,必胜。

输赢某种意义上是不断转化的。

由此,必输状态的下一个状态是必胜状态。必胜状态的下一个状态如果不是必输状态,则为若干状态相互组成,这里我个人称作吊打状态(我告诉你,我有X种方式来解决些问题)。

总结自代码以及个人作死:

对于单场(一行)游戏来说:

SG = 0,无力回天。

SG = 1,高手躺赢。

SG > 1,花式吊打。

因为这道题涉及到多场游戏,所以我们需要区分种类不同的花式吊打,比如花式吊打对手的时候可能是:无情吊打(SG = 2),残忍虐杀(花式吊打SG = 3),笑而不语(SG = 4)…………等等。

然后我们从0(必输)开始看看是怎么确定出上一个状态的。

先祖状态 SG = 神马都没有,当前状态 0

先祖状态SG = {0},当前状态 1

(问题来了,如何才能吊打对手?这局我想输就输,想赢就赢。)

先祖状态SG = {0, 1},当前状态 2

(似乎掌握了所有前置先祖状态才能推导出下一个状态,不能解决前面的所有状态,怎么能叫吊打?)

因此当前SG[now] = 先祖SG{a,b,c……}中从0开始的最长连续长度。

先祖SG为空,当前SG为0。

(其中先祖SG无重复,因为重复出现相当于某种一样上的等价先祖,取谁有一样)

个人总结SG定理:连续花式吊打数。

关于总场次的胜负判定:

对于各个场次,我发先手状态下若为:必胜 + 必胜

最终结果则为 必负。

因此:

必胜 + 必胜 = 必负。

必负 + 必负 = 必负。

必胜 + 必负 = 必胜。

此处直接转为疑惑运算(你要判断奇偶我也不拦你……):

1 ^ 1 = 0;

0 ^ 0 = 0;

1 ^ 0 = 0;

所以真正能决定某种状态(SG)变化的只有相同的状态(SG),结果的运算也就是见招拆招,所以就是不断地异或(真的,你要算奇偶我不会拦你的)

最终结果:SG[a1] ^ SG[a2] ^ ………… ^ SG[an]

对应单场游戏的结果:

0,默哀

1,躺赢

大于1,吊打

【代码 C++】

 #include <cstdio>
#include <cstring>
int SG[( << ) + ];
void rdy(){
int i, j, last, isUS[];
for (i = ; i < ( << ); ++i){
memset(isUS, , sizeof(isUS)); last = -;
for (j = ; j < ; ++j){
if (i&( << j)){
if (~last) isUS[SG[i ^ ( << j) ^ ( << last)]] = ;
}
else last = j;
}
for (j = ; isUS[j]; ++j);
SG[i] = j;
}
}
int main(){
rdy();
int t, n, m, p, now, s, w;
for (scanf("%d", &t); t; --t){
s = ;
for (scanf("%d", &n); n; --n){
now = ;
for (scanf("%d", &m); m; --m){
scanf("%d", &p); now |= << - p;
}
s ^= SG[now];
}
if (s) puts("YES");
else puts("NO");
}
return ;
}

HDU 5724 Chess(国际象棋)的更多相关文章

  1. HDU 5724 Chess (sg函数)

    Chess 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5724 Description Alice and Bob are playing a s ...

  2. HDU 5724 Chess(博弈论)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5724 [题目大意] 给出一个n行,每行有20格的棋盘,棋盘上有一些棋子,每次操作可以选择其中一个棋 ...

  3. HDU 5724 Chess(SG函数+状态压缩)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=5724 题意: 现在有一个n*20的棋盘,上面有一些棋子,双方每次可以选择一个棋子把它移动到其右边第一 ...

  4. hdu 5724 Chess 博弈sg+状态压缩

    Chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem De ...

  5. HDU 5724 Chess(SG函数)

    Chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  6. HDU 5724 Chess (状态压缩sg函数博弈) 2016杭电多校联合第一场

    题目:传送门. 题意:有n行,每行最多20个棋子,对于一个棋子来说,如果他右面没有棋子,可以移动到他右面:如果有棋子,就跳过这些棋子移动到后面的空格,不能移动的人输. 题解:状态压缩博弈,对于一行2^ ...

  7. HDU 5724 - Chess

    题意:    一个n行20列的棋盘. 每一行有若干个棋子.     两人轮流操作, 每人每次可以将一个棋子向右移动一个位置, 如果它右边有一个棋子, 就跳过这个棋子, 如果有若干个棋子, 就将这若干个 ...

  8. hdu 5724 Chess 博弈

    题目链接 一个n行20列的棋盘. 每一行有若干个棋子. 两人轮流操作, 每人每次可以将一个棋子向右移动一个位置, 如果它右边有一个棋子, 就跳过这个棋子, 如果有若干个棋子, 就将这若干个都跳过. 但 ...

  9. HDU 5724:Chess(博弈 + 状压)

    http://acm.hdu.edu.cn/showproblem.php?pid=5724 Chess Problem Description   Alice and Bob are playing ...

随机推荐

  1. android 学习随笔十五(Activity的生命周期与摧毁时返回数据 )

    1.Activity的生命周期 onCreate:创建时调用 onStart:在屏幕上可见,但是还没有获得焦点 onResume:可见并且获得焦点 onPause:可见,但是失去焦点 onStop:不 ...

  2. struts2 笔记03 异常支持、防止页面刷新和后退、方法验证

    Struts2对异常支持(声明式异常.自动的异常处理), 异常处理(运行期异常事务自动回滚) 1. 自定义异常类,继承RuntimeException或Exception实现构造方法. 2. 配置异常 ...

  3. java中==与equal()方法的区别

    ==比较的是对象的地址,也就是是否是同一个对象: equal比较的是对象的值. Integer r1 = new Integer(900);//定义r1整型对象Integer r2 = new Int ...

  4. jsp数据库连接出现乱码

    只要涉及中文的地方全部是乱码,解决办法:在数据库的数据库URL中加上 useUnicode=true&characterEncoding=GBK 就OK了. 之前所写,迁移至此 原文链接:ht ...

  5. Temporary Tables and the TableType Property [AX 2012]

    Temporary Tables and the TableType Property [AX 2012] 1 out of 1 rated this helpful - Rate this topi ...

  6. 时间戳转换成Date

    SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ); String date = for ...

  7. java IO和NIO 的区别

    Java NIO和IO的主要区别 下表总结了Java NIO和IO之间的主要差别. IO                NIO 面向流            面向缓冲 阻塞IO           非 ...

  8. [BIM]BIM中IDM介绍

    参考:http://blog.fang.com/25866228/10613454/articledetail.htm IDM的全称是Information Delivery Manual,信息交付手 ...

  9. ThinkPhp之分页

    function page($count,$pagesize=3){ $Page=new \Think\Page($count,$pagesize); //每页显示记录数 $Page->setC ...

  10. LA 4998 Simple Encryption

    题意:输入正整数$K_1(K_1 \leq 50000)$, 找一个$12$位正整数$K_2$(不能含有前导零)使得${K_1}^{K_2}\equiv K_2(mod10^{12})$. 例如,$K ...