HDU 5742 Chess SG函数博弈

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的棋盘
  每一行有若干棋子,A,B轮流移动棋子,每次可以选定一个棋子移动到右边第一个空格,直到不能移动的人败
题解:
  SG
  吧SG(1<<20)的状态全部预处理出来就可以了
  再把sg值全部异或起来,为0先手输
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e18+1LL;
const double Pi = acos(-1.0);
const int N = 5e5+, M = 2e5+, mod = 1e9+, inf = 2e9; int sg[<<],vis[];
void init() {
int NN = <<;
for(int i = NN; i >= ; --i) {
memset(vis,,sizeof(vis));
int last = -;
for(int j = ; j >= ; --j) {
if((i&(<<j)) == ) last = j;
else {
if(last == -) continue;
int now = i - (<<j) + (<<last);
vis[sg[now]] = ;
}
}
for(int j = ; j <= ; ++j) {
if(!vis[j]) {
sg[i] = j;
break;
}
}
}
}
int main() {
init();
int T,m,x,y;
scanf("%d",&T);
while(T--) {
scanf("%d",&m);
int ans = ;
for(int i = ; i <= m; ++i) {
int now = ;
scanf("%d",&x);
while(x--) {
scanf("%d",&y);
y--;
now += (<<(y));
}
ans ^= sg[now];
}
if(ans) puts("YES");
else puts("NO");
}
return ;
}
上一篇:ahoi2009维护序列


下一篇:远程过程调用概述-RMI简介