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. 第三篇 SQL Server安全主体和安全对象

    本篇文章是SQL Server安全系列的第三篇,详细内容请参考原文. 一般来说,你通过给主体分配对象的权限来实现SQL Server上的用户与对象的安全.在这一系列,你会学习在SQL Server实例 ...

  2. mysql修改表名,列名,列类型,添加表列,删除表列

    alter table test rename test1; --修改表名 ); --添加表列 alter table test drop column name; --删除表列 ) --修改表列类型 ...

  3. svn合并

    1.先去 aone里我的变更 里 重建 新分支 相当于重主干上拉代码下来2.然后再去 原来的分支里 swich切换到新分支3.在原来的分支里 merge 到新分支的url4.选择最早的 version ...

  4. UIBarButtonItem变弹簧

    UIBarButtonItem * spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystem ...

  5. JavaOOP QuickHit项目分析

    项目需求:游戏等级6级,随机字符串每级长度不同.每升一级减少比较次数,但是字符串长度相应增加!每级总分数不同,如果游戏中途输入错误则游戏退出!玩家每次在规定时间内输入字符串的同时,打印出游戏难度等级. ...

  6. springday04-go2

    练习:计算一个人的bmi指数.算法如下: 身高 单位是米 比如1.70 体重 单位是公斤 比如90 bmi指数 = 体重/身高/身高 如果bmi>24过重,否则正常.视图需要两个,一个是bmi_ ...

  7. ios blog

    转得一个朋友的博客,大家可以看哈(主要时国外的) 主要分开发教程.示例项目.UI设计.问题解决几块. 开发教程: 即便过了入门阶段,还是要经常看看一些不错的实例教程. .http://mobile.t ...

  8. linux中关于php和nginx用户权限的一些东西

    当我们启动nginx之后, 会有两个nginx进程(一个是master, 一个是worker). master的用户身份是root, worker用户的身份是nobody. 在nginx中可以修改 w ...

  9. viewpager+fragment+HorizontalScrollView详细版

    XML布局 <HorizontalScrollView            android:id="@+id/hsv"            android:layout_ ...

  10. notification 通知

    1. 定义一个方法 -(void) update{ } 2. 对象注册,并关连消息 [[NSNotificationCenter defaultCenter]addObserver:self sele ...