HDU 5724 Chess (sg函数)
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函数)的更多相关文章
- HDU 5742 Chess SG函数博弈
Chess Problem Description Alice and Bob are playing a special chess game on an n × 20 chessboard. ...
- HDU 5724 Chess(国际象棋)
HDU 5724 Chess(国际象棋) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- S-Nim HDU 1536 博弈 sg函数
S-Nim HDU 1536 博弈 sg函数 题意 首先输入K,表示一个集合的大小,之后输入集合,表示对于这对石子只能去这个集合中的元素的个数,之后输入 一个m表示接下来对于这个集合要进行m次询问,之 ...
- HDU 5724 Chess(SG函数+状态压缩)
http://acm.split.hdu.edu.cn/showproblem.php?pid=5724 题意: 现在有一个n*20的棋盘,上面有一些棋子,双方每次可以选择一个棋子把它移动到其右边第一 ...
- HDU 5724 Chess(SG函数)
Chess Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submi ...
- hdu 1848 简单SG函数
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1848 Problem Description 任何一个大学生对菲波那契数列(Fibonacci num ...
- hdu 1809 求SG函数
A New Tetris Game(2) Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- HDU 5724 Chess (状态压缩sg函数博弈) 2016杭电多校联合第一场
题目:传送门. 题意:有n行,每行最多20个棋子,对于一个棋子来说,如果他右面没有棋子,可以移动到他右面:如果有棋子,就跳过这些棋子移动到后面的空格,不能移动的人输. 题解:状态压缩博弈,对于一行2^ ...
- hdu 5724 Chess 博弈sg+状态压缩
Chess Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem De ...
随机推荐
- C++STL 之排列
固然我们可以自己使用递归编写全排列程序,但是既然STL里面已将有了这个功能为什么不直接用呢,下面就写一下直接使用C++ STL生成全排序的程序 函数名:next_permutation 包含头文件:a ...
- bzoj1834: [ZJOI2010]network 网络扩容
努力看了很久样例一直过不了...然后各种输出中间过程啊巴拉巴拉弄了1h,没办法了...然后突然想到啊原来的边可以用啊为什么不用...于是A了...感人肺腑 #include<cstdio> ...
- css3的背景多重运用
效果图: 简单代码: http://www.developerdrive.com/2013/08/introducing-css3-multiple-backgrounds/ 演示地址: http:/ ...
- jquery 字符串转json
这里考虑的都是服务器返回JSON形式的字符串的形式 代码如下: var data=" { root: [ {name:'1',value:'0'}, {name:'6101',value:' ...
- ios 编译openssl支持arm64(转)
最近在编译支付宝 快捷支付(无线) ios 端的时候发现demo不支持arm64.在网上找了下,看到客服说是openssl的库文件不支持arm64,于是自己编译了支持arm64的库文件,发现还是不行, ...
- shell 中grep命令详解
用‘grep’搜索文本文件如果您要在几个文本文件中查找一字符串,可以使用‘grep’命令.‘grep’在文本中搜索指定的字符串.举个例子:假设您正在‘/usr/src/linux/Documentat ...
- 一天一个Java基础——泛型
这学期的新课——设计模式,由我仰慕已久的老师传授,可惜思维过快,第一节就被老师挑中上去敲代码,自此在心里烙下了阴影,都是Java基础欠下的债 这学期的新课——算法设计与分析,虽老师不爱与同学互动式的讲 ...
- 【大数模板】C++大数类 大数模板
分别使用C++中的运算符重载的方法来实现大数之间的数学运算,包括加法.减法.乘法.除法.n次方.取模.大小比较.赋值以及输入流.输出流的重载. 感觉很麻烦... [代码] #include<io ...
- (function(){})()这个是什么?有不明觉厉的感觉么?
今天在RunJs上看到一个人分享的一个jquery代码,写的是jquery弹性滑动效果.不过,看着看着,发现一句代码(function{})(),突然有种不明觉厉的感觉. 事实上,只是因为我们没有用过 ...
- mysql open files
错误信息如下: ..... 150905 13:10:17 [ERROR] /usr/local/mysql/bin/mysqld: Can't open file: './mytest/t1.frm ...