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 多线程Thread和Runnable

    Thread: class MyThread extends Thread { private int ticketsCont=5; //一共有5张火车票 private String name; / ...

  2. Java foreach

    foreach循环也叫增强型的for循环,或者叫foreach循环. foreach循环是JDK5.0的新特性(其他新特性比如泛型.自动装箱等). import java.util.Arrays; p ...

  3. 点云匹配和ICP算法概述

    Iterative Closest Point (ICP) [1][2][3] is an algorithm employed to minimize the difference between ...

  4. IIS 相关

    Restart IIS: run "iisreset" command check port usage: netstat -ano How to set up SSL in II ...

  5. iOS 判断字符串中含有某个字符串 rangeOfString

    //判断roadTitleLab.text 是否含有qingjoin if([roadTitleLab.text rangeOfString:@"qingjoin"].locati ...

  6. Effective C++ 3.资源管理

    //条款13:以对象管理资源 // 1.C++程序中最常使用的资源就是动态分配内存,并且还包括文件描述器,互斥锁,GDI对象.数据库连接.网络socket等.不管哪一种资源,当不再使用的时候必须将其归 ...

  7. PostgreSQL 三节点集群故障模拟及恢复

    PostgreSQL 三节点集群故障模拟及恢复 (postgreSQL9.5.1) 正常状态: 10.2.208.10:node1:master 10.2.208.11:node2:standby1同 ...

  8. Climbing Stairs - Print Path

    stair climbing, print out all of possible solutions of the methods to climb a stars, you are allowed ...

  9. web 前端:连接mysql中文乱码问题的解决办法

    当使用tomcat部署servlet连接时,总是出现各种原因的中文乱码问题.有的是因为mysql的默认字符集的问题,有的是客户端的字符配置问题. 现在对于这两种问题进行配置,来解决乱码. 服务器端(m ...

  10. 树形DP 2013多校8(Terrorist’s destroy HDU4679)

    题意: There is a city which is built like a tree.A terrorist wants to destroy the city's roads. But no ...