HDU 5724:Chess(博弈 + 状压)
http://acm.hdu.edu.cn/showproblem.php?pid=5724
Chess
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.
题意:两个人下棋,棋盘有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(博弈 + 状压)的更多相关文章
- hdu 5724 Chess 博弈sg+状态压缩
Chess Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem De ...
- hdu 5724 Chess 博弈
题目链接 一个n行20列的棋盘. 每一行有若干个棋子. 两人轮流操作, 每人每次可以将一个棋子向右移动一个位置, 如果它右边有一个棋子, 就跳过这个棋子, 如果有若干个棋子, 就将这若干个都跳过. 但 ...
- HDU 5724 Chess(国际象棋)
HDU 5724 Chess(国际象棋) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- hdu 3247 AC自动+状压dp+bfs处理
Resource Archiver Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 100000/100000 K (Java/Ot ...
- hdu 2825 aC自动机+状压dp
Wireless Password Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- hdu 4778 Gems Fight! 状压dp
转自wdd :http://blog.csdn.net/u010535824/article/details/38540835 题目链接:hdu 4778 状压DP 用DP[i]表示从i状态选到结束得 ...
- HDU 3605 Escape(状压+最大流)
Escape Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Sub ...
- HDU 5765 Bonds 巧妙状压暴力
题意:给一个20个点无向连通图,求每条边被多少个极小割集包括 分析:极小割集是边的集合,很显然可以知道,极小割集恰好吧原图分成两部分(这个如果不明白可以用反证法) 然后就是奉上官方题解:http:// ...
- HDU 5765 Bonds(状压DP)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5765 [题目大意] 给出一张图,求每条边在所有边割集中出现的次数. [题解] 利用状压DP,计算不 ...
随机推荐
- 框架基础——全面解析Java注解
为什么学习注解? 学习注解有什么好处? 学完能做什么? 答:1. 能够读懂别人写的代码,特别是框架相关的代码: 2. 让编程更加简洁,代码更加清晰: 3. 让别人高看一眼. spring.mybati ...
- UICollection 重排 和汉字拼音
http://nshint.io/blog/2015/07/16/uicollectionviews-now-have-easy-reordering/ NSMutableString *str = ...
- duplicate命令创建physical standby数据库报RMAN-03015 ORA-17628
The following error is reported trying to create a Physical Standby database using "duplicate f ...
- 使用gradle创建java程序
创建一个Java项目 我们可以使用Java插件来创建一个Java项目,为了做到这点,我们需要把下面这段语句加入到build.gradle文件中: 1 apply plugin: 'java' 就是这样 ...
- SQL 2012 连接失败
- 压缩 & 解压缩 命令汇总:tar、zip & unzip、
1. tar命令详解 格式:tar [-cxtzjvfpPN] 文件与目录 -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追加文件 -u:更新原压缩包中的文件 这五 ...
- Myeclipse10编写jsp时出现 Multiple annotations found at this line:
今天,老师讲完课做了一个小练习,就是编写一个jsp页面.写完后,我发现少些了点东西,我准备使用<% %>添加是发现,报错了 Multiple annotations found at th ...
- bzoj3489 A simple rmq problem 可持久化树套树
先预处理出两个个数组pre,next.pre[i]表示上一个与i位置数字相同的位置,若不存在则设为0:next[i]表示下一个与i位置数字相同的位置,若不存在则设为n+1.那么一个满足在区间[L,R] ...
- Fresco源码解析 - 创建一个ImagePipeline(一)
在Fresco源码解析 - 初始化过程分析章节中, 我们分析了Fresco的初始化过程,两个initialize方法中都用到了 ImagePipelineFactory类. ImagePipeline ...
- Windows7 IIS7.5 HTTP Error 503 The service is unavailable 另类解决方案
这篇文章是在你看了别的解决方案仍然解决不了之后才有用. 所以再未用别的解决方案之前,用了该解决方案依然无效的话,请自己看着办. 原创: .net2.0和.net3.5的应用程序池请在开始菜单打开VS2 ...