Chess

题目链接:

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 的棋盘上有若干棋子,棋子每次只能水平地移动到右边第一个空位.
两人以最优策略轮流走,求先手是否能取胜.


##题解:

只考虑单行的情况,由于最多只有20个位置,所以可以状态压缩.
对于每个状态,考虑它能够转移到的状态,求sg值即可.


##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define eps 1e-8
#define maxn 101000
#define mod 100000007
#define inf 0x3f3f3f3f
#define mid(a,b) ((a+b)>>1)
#define IN freopen("in.txt","r",stdin);
using namespace std;

int sg[1<<21];

bool vis[25];

int get_sg(int state) {

/右边最后一个空位置/

int last = -1;

memset(vis, 0, sizeof(vis));

for(int i=0; i<20; i++) {

if(state & (1<<i)) {

if(last == -1) continue;

int nextstate = state ^ (1<<last) ^ (1<<i);

vis[sg[nextstate]] = 1;

} else {

last = i;

}

}

for(int i=0; i<25; i++) if(!vis[i]) {
return i;
}

}

int main(int argc, char const *argv[])

{

//IN;

for(int s=0; s<(1<<20); s++)
sg[s] = get_sg(s); int t; cin >> t;
while(t--)
{
int n; scanf("%d", &n); int ans = 0;
while(n--) {
int m; scanf("%d", &m);
int state = 0;
while(m--) {
int x; scanf("%d", &x);
state |= 1 << (20-x);
}
ans ^= sg[state];
} if(ans) puts("YES");
else puts("NO");
} return 0;

}

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 5724 Chess(SG函数)

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

  6. hdu 1848 简单SG函数

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

  7. hdu 1809 求SG函数

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

  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. C++STL之map的基本操作

    STL中基本的关联式容器有map和set,它们都是以红黑树作为其底层的结构,具有非常高的查找.删除效率,内容会按照键值自动排序. 使用map的注意事项: 1.关联式容器的键值是不允许修改的,所以永远不 ...

  2. C++ 中字符串标准输入的学习及实验

    声明:下面实验中[]里面表示要输入里面的符号,[]符号本身并未输入 1.cin>> cin使用空白(空格.制表符.回车)来确定字符串的结束位置. cin会将换行符留在输入输出队列中. #i ...

  3. 8天学通MongoDB——第六天 分片技术

    在mongodb里面存在另一种集群,就是分片技术,跟sql server的表分区类似,我们知道当数据量达到T级别的时候,我们的磁盘,内存 就吃不消了,针对这样的场景我们该如何应对. 一:分片 mong ...

  4. UVa 1640 (计数) The Counting Problem

    题意: 统计[a, b]或[b, a]中0~9这些数字各出现多少次. 分析: 这道题可以和UVa 11361比较来看. 同样是利用这样一个“模板”,进行区间的分块,加速运算. 因为这里没有前导0,所以 ...

  5. bzoj1927: [Sdoi2010]星际竞速

    跟上一题几乎一样... #include<cstdio> #include<cstring> #include<iostream> #include<algo ...

  6. RegExp类型和text()方法

    ECMAScript通过RegExp类型来支持正则表达式 RegExp 实例方法:text() 它接受一个字符串参数,在模式与该参数匹配的情况下返回true,否则返回false,通常用在if语句中 / ...

  7. [Everyday Mathematics]20150207

    求极限 $$\bex \lim_{x\to+\infty}\sex{\sqrt{x+\sqrt{x+\sqrt{x^\al}}}-\sqrt{x}},\quad\sex{0<\al<2}. ...

  8. 用Apache Kafka构建流数据平台的建议

    在<流数据平台构建实战指南>第一部分中,Confluent联合创始人Jay Kreps介绍了如何构建一个公司范围的实时流数据中心.InfoQ前期对此进行过报道.本文是根据第二部分整理而成. ...

  9. Strom-7 Storm Trident 详细介绍

    一.概要 1.1 Storm(简介)      Storm是一个实时的可靠地分布式流计算框架.      具体就不多说了,举个例子,它的一个典型的大数据实时计算应用场景:从Kafka消息队列读取消息( ...

  10. codeforces 675D Tree Construction set

    转自:http://blog.csdn.net/qwb492859377/article/details/51447350 #include <stdio.h> #include < ...