http://acm.hdu.edu.cn/showproblem.php?pid=5724

Chess

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

题意:两个人下棋,棋盘有n行20列,每一行有m个棋子分别位于p,棋子只能往右走,如果有相邻的棋子,可以跳过这个棋子,当不能动的时候就输了,问先手能不能赢。

思路:因为只有20列,可以状态压缩,打表枚举一行的所有状态,然后所有行状态的sg值异或起来就是答案。

 #include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 21 int sg[<<N];
bool vis[]; int get_sg(int now)
{
memset(vis, , sizeof(vis));
int i;
//终止点为第20列,在这里的第20列为i = 0,因为i = 0的时候为必败点P
//从第一列开始枚举,找到属于该状态的列,并且在该列下枚举,找到不属于该状态的列
for(i = ; i >= ; i--) {
if(now & ( << i)) {
int tmp = now;
for(int j = i - ; j >= ; j--) {
if(!(now & ( << j))) {
//这里异或的结果相当于第20-i列的棋子走到第20-j列的状态
tmp ^= ( ( << i) ^ ( << j) );
vis[sg[tmp]] = ;
break;
}
}
}
}
for(i = ; i <= ; i++)
if(!vis[i]) {
sg[now] = i;
break;
}
} int main()
{
sg[] = ;
for(int i = ; i <= ( << ); i++)
get_sg(i);
//打表枚举所有状态
int t;
scanf("%d", &t);
while(t--) {
int n, ans = , m, x, y;
scanf("%d", &n);
for(int i = ; i <= n; i++) {
scanf("%d", &m);
x = ;
for(int j = ; j <= m; j++) {
scanf("%d", &y);
x |= ( << ( - y));
//x = 所有有棋子的列加起来的状态
}
ans ^= sg[x];
//最后的结果是所有行异或
// printf("ANS : %d\n", ans);
}
if(ans == ) puts("NO");
else puts("YES");
}
return ;
}

HDU 5724:Chess(博弈 + 状压)的更多相关文章

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

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

  2. hdu 5724 Chess 博弈

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

  3. HDU 5724 Chess(国际象棋)

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

  4. hdu 3247 AC自动+状压dp+bfs处理

    Resource Archiver Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Ot ...

  5. hdu 2825 aC自动机+状压dp

    Wireless Password Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  6. hdu 4778 Gems Fight! 状压dp

    转自wdd :http://blog.csdn.net/u010535824/article/details/38540835 题目链接:hdu 4778 状压DP 用DP[i]表示从i状态选到结束得 ...

  7. HDU 3605 Escape(状压+最大流)

    Escape Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Sub ...

  8. HDU 5765 Bonds 巧妙状压暴力

    题意:给一个20个点无向连通图,求每条边被多少个极小割集包括 分析:极小割集是边的集合,很显然可以知道,极小割集恰好吧原图分成两部分(这个如果不明白可以用反证法) 然后就是奉上官方题解:http:// ...

  9. HDU 5765 Bonds(状压DP)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5765 [题目大意] 给出一张图,求每条边在所有边割集中出现的次数. [题解] 利用状压DP,计算不 ...

随机推荐

  1. Java遇见HTML——JSP篇之JSP状态管理

    一.http协议的无状态 无状态性是指,当浏览器发送请求给服务器的时候,服务器响应客户端请求.但是当同一个浏览器再次发送请求给服务器的时候,服务器并不知道他就是刚才的那个浏览器.简单的说,就是服务器不 ...

  2. sqlplus登陆

    cd \sqlplus sys@test_id as sysdba 切换用户SQL> connect system@test_id

  3. curl获得http响应码 302 和绑定host

    shell curl 取得HTTP返回的状态 获取状态码 curl -I -m 10 -o /dev/null -s -w %{http_code} www.baidu.com 获取时间   curl ...

  4. MySQL的半同步复制监控

    (1)master端 >show variables like 'rpl_semi_sync%'; +------------------------------------+-------+ ...

  5. Xcode 遇到 App Transport Security has blocked a cleartext HTTP 错误

    今天用Xcode  创建新项目用到 URL 发送请求时,报下面的错: “App Transport Security has blocked a cleartext HTTP (http://) re ...

  6. 配置suse自动化安装

    配置suse自动化安装 作者:尹正杰   版权声明:原创作品,谢绝转载!否则将追究法律责任.       前言:不知道你习惯用那款虚拟器,我用的是VMware Workstation,别问我为什么,因 ...

  7. Python和Ruby开发中源文件中文注释乱码的解决方法(Eclipse和Aptana Studio3均适用)

    Eclipse的设置(Aptana Studio3与Eclipse基本完全相同,此处略) window->preferences->general->editors->text ...

  8. Sqlserver列出所有数据库名,表名,字段名

    Sqlserver列出所有数据库名,表名,字段名   1.获取所有数据库名:   ? 1 SELECT Name FROM Master..SysDatabases ORDER BY Name   注 ...

  9. 实验一 操作系统模仿cmd

    实验一.命令解释程序的编写 专业:商软(2)班   姓名:王俊杰  学号:201406114252 一.        实验目的 (1)掌握命令解释程序的原理: (2)掌握简单的DOS调用方法: (3 ...

  10. yii2验证码的使用

    1.控制器中 public function actions()     {         return [             'captcha' => [               ...