hdu 5724 Chess (SG函数)
来源:互联网 发布:江苏海纳船务知乎 编辑:程序博客网 时间:2024/05/16 01:12
Chess
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1935 Accepted Submission(s): 842
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 integerT(T≤100) , indicates the number of test cases.
For each test case, the first line contains a single integern(n≤1000) , the number of lines of chessboard.
Thenn 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.
The first line contains an integer
For each test case, the first line contains a single integer
Then
Output
For each test case, output one line of “YES” if Alice can win the game, “NO” otherwise.
Sample Input
212 19 2021 191 18
Sample Output
NOYES
题意:有一个棋盘,每行一些位置有棋子。右边无棋子时棋子可以向右移动,有棋子的可以跳到他后面第一个空位。不能出棋盘,不能操作的算输。问先手是否能赢。
解法是SG函数。每一行看作一种状态,因为一行最多只有20个棋子,把有棋子的地方设为1,没有棋子的地方设为0,构成一个二进制数,转换为十进制数作为一种状态。根据题意可以找到它可以由哪些状态转移而来,根据这个就可以生成SG函数映射的每一个值。
CODE
#include <bits/stdc++.h>using namespace std;const int N = (1<<20)+10;int SG[N];int sg(int x){ if(SG[x] != -1) return SG[x]; bool vis[500] = {false}; for(int i = 0;i <= 19;i++){ if((1<<i)&x){ int tmp = x; for(int j = i-1;j >= 0;j--){///可以由右边第一个非0位置进行转移 if(((1<<j)&x) == 0){ tmp ^= ((1<<i)^(1<<j)); vis[sg(tmp)] = true; break; } } } } for(int i = 0;;i++) if(!vis[i]) return SG[x] = i;}int main(void){ int T; scanf("%d",&T); memset(SG,-1,sizeof SG); SG[1] = 0; for(int i = 0;i <= (1<<20);i++) SG[i] = sg(i); while(T--){ int n; scanf("%d",&n); int ans = 0; for(int i = 1;i <= n;i++){ int m; scanf("%d",&m); int tmp = 0; for(int j = 1;j <= m;j++){ int t; scanf("%d",&t); tmp ^= (1<<(20-t)); ///因为是反过来的,所以需要翻转 } ans ^= SG[tmp]; } if(ans) puts("YES"); else puts("NO"); } return 0;}
0 0
- HDU 5724 Chess(博弈,SG函数)
- hdu 5724 Chess(SG函数)
- hdu 5724 Chess (SG函数)
- hdu 5724 - chess (sg函数)
- hdu 5724 Chess(sg函数)
- hdu 5724 Chess SG函数
- (组合博弈)(sg函数模版)HDU 5724 Chess
- hdu 5724 Chess (sg函数 + 状态压缩)
- HDU 5724 Chess (博弈 状压+sg函数)
- HDU 5724-Chess-博弈论-sg函数
- HDU 5724 Chess(SG函数 Nim博弈)
- HDU 5724 chess sg函数学习
- hdu 5724 Chess [状压+SG函数]【博弈】
- HDU-5724 Chess(SG函数+状压)
- hdu 5742 chess SG函数
- hdu 5724 Chess(sg博弈)
- HDU 5724 Chess (状压+sg)
- HDU 5724 Chess 从懵逼到学会 SG函数
- 设计模式:strategy
- google笔试题 -- 根据已知数列得到不能组合求和的最小数
- 关于CPU Cache——程序猿需要知道的那些事
- 6. ZigZag Conversion
- 如何正确的开启一个项目
- hdu 5724 Chess (SG函数)
- 3076. 【备战NOIP2012图论专项模拟试题】位图 (Standard IO)
- Round Numbers--杨辉三角,组合数学
- java基础学习之Layout2
- HDU 1711 裸KMP,附KMP模板
- 图结构练习——判断给定图是否存在合法拓扑序列
- 【51Nod】1242 - 斐波那契数列的第N项(矩阵快速幂)
- Java与Java web开发教程网站之IBM developer
- NVL,NVL2