HDU 5724 Chess(SG函数)

Chess

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2605    Accepted Submission(s): 1092

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

解题思路:

  此题相较一般的组合博弈规则上有所不同,无法使用一般的Nim博弈、威佐夫博弈那样直接用公式进行推导而取得结果,所以一般使用SG函数来处理此类问题,而此题由于状态相对单一(即一个点只有有棋子和没有棋子两种状态),故可用一bit来表示在一行中一个点有没有棋子,有棋子为1,没有棋子为0,因为每一行的棋子数量不定,故有可能一行20个位置都是棋子为了表示这种的不同的情况,进行状态压缩,用一个int来表示这种情况显然是更好的选择。其中int i=1即代表一行只有一个棋子,而此棋子在最右边的情况,0到(2^20-1)就代表全了所有的可能,存于SG中,之后读取并Nim博弈的异或看结果就好了。

代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstdlib>
 5 #include <cstring>
 6 #include <cmath>
 7 using namespace std;
 8 int sg[(1<<20)+1000];
 9 int main(){
10
11
12     for(int i = 1;i < (1<<20); i++){
13         int h[25];
14         memset(h, -1, sizeof(h));
15         int last = -1;
16         for(int j = 0; j < 20; j++){
17
18             if(!((i >> j) & 1))
19                 last = j;
20             if(((i >> j) & 1)){
21                 if(last != -1){
22                     h[sg[(i ^ (1 << j)) ^ (1 << last)]]=1;
23                 }
24             }
25         }
26         int j=0;
27         while(h[j] != -1) j++;
28         sg[i]=j;
29     }
30     int T;
31     scanf("%d", &T);
32     while(T--){
33         int n, tmp, m, ans=0;
34         scanf("%d", &n);
35         for(int i = 1; i <= n; i++){
36             scanf("%d", &m);
37             tmp = 0;
38             for(int j = 1; j <= m; j++){
39                 int x;
40                 scanf("%d", &x);
41                 tmp ^= 1 << (20 - x);
42             }
43             ans ^= sg[tmp];
44
45         }
46         if(ans)
47             puts("YES");
48         else
49             puts("NO");
50     }
51 }
时间: 2024-12-18 13:40:36

HDU 5724 Chess(SG函数)的相关文章

HDU 5724 Chess(国际象棋)

HDU 5724 Chess(国际象棋) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Description 题目描述 Alice and Bob are playing a special chess game on an n × 20 chessboard. There are several chesses on the chessboard. They can mo

hdu 1848 简单SG函数

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1848 Problem Description 任何一个大学生对菲波那契数列(Fibonacci numbers)应该都不会陌生,它是这样定义的:F(1)=1;F(2)=2;F(n)=F(n-1)+F(n-2)(n>=3);所以,1,2,3,5,8,13……就是菲波那契数列.在HDOJ上有不少相关的题目,比如1005 Fibonacci again就是曾经的浙江省赛题.今天,又一个关于Fibonacc

HDU 5724 Chess

因为一行最多只有20个数,也就是说只有(1<<20)种状态,向右移动表示小的数推向了大的数.可以用SG函数预处理出所有情况.然后把每一行的SG函数值异或一下,非零则必胜,否则输. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #include<map> #include<set> #

HDOJ 5724 博弈SG函数

链接: http://blog.csdn.net/tc_to_top/article/details/51958964 题意: n行20列的棋盘,对于每行,如果当前棋子右边没棋子,那可以直接放到右边,如果有就跳过放到其后面的第一个空位子,A先操作,最后谁无法操作则输,给定每行棋子状态,问先手是否必胜 题解: 组合博弈问题,直接sg函数,因为列只有20,可以状压搞,枚举每个状态,找到该状态下可行的操作然后标记 代码: 31 int sg[1 << 21]; 32 int vis[21]; 33

HDU 5724 Chess(博弈论)

[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5724 [题目大意] 给出一个n行,每行有20格的棋盘,棋盘上有一些棋子,每次操作可以选择其中一个棋子,将其移至最左端的空位,两个人轮流操作,无法操作者输,判断游戏胜负. [题解] 首先对于单行20格的游戏,这是一个NIM游戏,将20格的情况状态压缩,对于每种情况递归求其mex集合,计算其sg值,sg值为0的状态为必败态. 而对于可以拆分为多组NIM游戏的游戏,其sg值为拆分出的多组游戏的sg值的

hdu 5724 Chess 博弈

题目链接 一个n行20列的棋盘. 每一行有若干个棋子. 两人轮流操作, 每人每次可以将一个棋子向右移动一个位置, 如果它右边有一个棋子, 就跳过这个棋子, 如果有若干个棋子, 就将这若干个都跳过. 但是棋子不能移出边界. 如果没有办法移动了, 就算输. 问你先走的能否赢. 只有20列, 所以预处理出所有状态的sg值. 然后直接异或就好了. 然后sg[(1<<20)-1] = 0, 这是必输态, 其他的都可以dfs出来, 具体看代码. #include <bits/stdc++.h>

Alice and Bob HDU - 4111 (SG函数)

Alice and Bob are very smart guys and they like to play all kinds of games in their spare time. The most amazing thing is that they always find the best strategy, and that's why they feel bored again and again. They just invented a new game, as they

HDU 1524 - A Chess Game(sg函数 + dfs)

A Chess Game Problem Description Let's design a new chess game. There are N positions to hold M chesses in this game. Multiple chesses can be located in the same position. The positions are constituted as a topological graph, i.e. there are directed

HDU ACM 1524 A Chess Game-&gt;博弈(SG函数)

题意:一个有向无环图上有n个顶点,每一个顶点都可以放一个棋子或不放,有两个人,每次根据这个图只能将任意一颗棋子移动一步,如果到某一步玩家不能移动时,那么这个人就输. 分析: 1.有向无环图的博弈,dfs把所有顶点的SG值都计算出来,然后对每个棋子的SG值进行异或运算,为0就是先手必败,否则就是先手必胜. 2.如果某个人移动后,所有棋子都在出度为0的顶点,那么他必败. SG函数简介: a.对于给定的有向无环图,定义图中每个顶点的Sprague-Grundy函数g如下:g(x) = mex{ g(y