Chess

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2605    Accepted Submission(s): 1092

Problem 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
 
解题思路:
  此题相较一般的组合博弈规则上有所不同,无法使用一般的Nim博弈、威佐夫博弈那样直接用公式进行推导而取得结果,所以一般使用SG函数来处理此类问题,而此题由于状态相对单一(即一个点只有有棋子和没有棋子两种状态),故可用一bit来表示在一行中一个点有没有棋子,有棋子为1,没有棋子为0,因为每一行的棋子数量不定,故有可能一行20个位置都是棋子为了表示这种的不同的情况,进行状态压缩,用一个int来表示这种情况显然是更好的选择。其中int i=1即代表一行只有一个棋子,而此棋子在最右边的情况,0到(2^20-1)就代表全了所有的可能,存于SG中,之后读取并Nim博弈的异或看结果就好了。
 
代码:
 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
int sg[(<<)+];
int main(){ for(int i = ;i < (<<); i++){
int h[];
memset(h, -, sizeof(h));
int last = -;
for(int j = ; j < ; j++){ if(!((i >> j) & ))
last = j;
if(((i >> j) & )){
if(last != -){
h[sg[(i ^ ( << j)) ^ ( << last)]]=;
}
}
}
int j=;
while(h[j] != -) j++;
sg[i]=j;
}
int T;
scanf("%d", &T);
while(T--){
int n, tmp, m, ans=;
scanf("%d", &n);
for(int i = ; i <= n; i++){
scanf("%d", &m);
tmp = ;
for(int j = ; j <= m; j++){
int x;
scanf("%d", &x);
tmp ^= << ( - x);
}
ans ^= sg[tmp]; }
if(ans)
puts("YES");
else
puts("NO");
}
}
 
 

HDU 5724 Chess(SG函数)的更多相关文章

  1. HDU 5742 Chess SG函数博弈

    Chess Problem Description   Alice and Bob are playing a special chess game on an n × 20 chessboard. ...

  2. HDU 5724 Chess(国际象棋)

    HDU 5724 Chess(国际象棋) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  3. S-Nim HDU 1536 博弈 sg函数

    S-Nim HDU 1536 博弈 sg函数 题意 首先输入K,表示一个集合的大小,之后输入集合,表示对于这对石子只能去这个集合中的元素的个数,之后输入 一个m表示接下来对于这个集合要进行m次询问,之 ...

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

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

  5. hdu 1848 简单SG函数

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1848 Problem Description 任何一个大学生对菲波那契数列(Fibonacci num ...

  6. hdu 1809 求SG函数

    A New Tetris Game(2) Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  7. HDU 5724 Chess (sg函数)

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

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

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

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

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

随机推荐

  1. css用hover制作下拉菜单

    首先我们的需求就是 制作一个鼠标移动到某个区域就会有下拉菜单的弹出,这样会有更多的子类内容,示例代码如下: <!DOCTYPE html> <html lang="en&q ...

  2. 使用 WireShark 分析 TCP/IP 三次握手 和 四次挥手

    TCP 三次握手 示意图 Wireshark 抓包注意事项 为了演示一个TCP三次握手建立连接的过程,我们通过 Chrome 访问一个网页. 已知 HTTP 协议就是建立在TCP链接上的 比如访问以下 ...

  3. hdu 1712 (分组背包)

    http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=17676 这个问题让我对分组背包更清晰了一点,主要是这个问题: 使用一维数组的 ...

  4. 封装hiredis——C++与redis对接(一)(string的SET与GET操作)

    在菜鸟教程自学了redis,总想着像Mysql一样,在C/C++中进行对接.于是查询了一些资料,最后找到了hiredis.然而直接用它的话,难免有点不方便.于是,对其进行封装. hiredis直接去g ...

  5. DOM节点常见的属性及操作

    (1)常见节点属性 childNodes      子节点 nodeList children      子节点(元素节点) HTMLCollection parentNode       父节点 p ...

  6. ElementUI组件库常见方法及问题汇总(持续更新)

    本文主要介绍在使用ElementUI组件库的时候,常遇见的问题及使用到的方法,汇总记录便于查找. 1.表单 阻止表单的默认提交 <!-- @submit.native.prevent --> ...

  7. js原生获取css属性

    原文参考http://blog.csdn.net/lzding/article/details/46317777 1.写在dom上的属性,内联样式 <div id="box" ...

  8. mongodb添加登陆验证

    mongodb添加登陆验证 转载地址 清空log,db目录 mongod --auth --logpath "D:\mongodb\log\log.log" --logappend ...

  9. 【Python】raw转义字符

    r"hi" 这里字符串前面加了r,是raw的意思,它表示对字符串不进行转义.为什么要加这个?你可以试试print "\bhi"和r"\bhi" ...

  10. jdk1.8 对数组及arrays类对数组的操作与增强

    数组的初始化有两种方式 静态初始化: 初始化时由程序员显示置顶每个数组的初始值,由系统决定数组长度.如: int[] a1 = new int[] {1,2,3,4}; 动态初始化:初始化时由程序员只 ...