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. js、javascript正则表达式验证身份证号码

    function isCardNo(card) { // 身份证号码为15位或者18位,15位时全为数字,18位前17位为数字,最后一位是校验位,可能为数字或字符X var reg = /(^\d{1 ...

  2. Android的底层库libutils介绍

    第一部分 libutils概述 libutils是Android的底层库,这个库以C++实现,它提供的API也是C++的.Android的层次的C语言程序和库,大都基于libutils开发. libu ...

  3. 转 intent常用功能

    1.从google搜索内容Intent intent = new Intent();intent.setAction(Intent.ACTION_WEB_SEARCH);intent.putExtra ...

  4. java实现DES算法

    import java.util.UUID; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypt ...

  5. CodeForces 489D Unbearable Controversy of Being

    题意: 给出一个n个节点m条边的有向图,求如图所示的菱形的个数. 这四个节点必须直接相邻,菱形之间不区分节点b.d的个数. 分析: 我们枚举每个a和c,然后求出所有满足a邻接t且t邻接c的节点的个数记 ...

  6. IIS Server is too busy 解决方法(IIS6)

    Server is too busy意思是服务器繁忙,资源不够用 为什么会出现这个问题呢? 因为服务器的配置不同,所能承受的压力不同. 而服务器默认对链接数,线程数等有设置,但这个设置太小,基本不够用 ...

  7. POJ 1904 King's Quest ★(强连通分量:可行完美匹配边)

    题意 有n个女生和n个男生,给定一些关系表示男生喜欢女生(即两个人可以结婚),再给定一个初始匹配,表示这个男生和哪个女生结婚,初始匹配必定是合法的.求每个男生可以和哪几个女生可以结婚且能与所有人不发生 ...

  8. Android Dialog使用举例

    在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框,在我们使用Android的过程中,我归纳了一 ...

  9. Java [Leetcode 337]House Robber III

    题目描述: The thief has found himself a new place for his thievery again. There is only one entrance to ...

  10. Java [Leetcode 119]Pascal's Triangle II

    题目描述: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return ...