您的位置:首页 > 大数据 > 人工智能

HDU 5274 Chess(SG博弈)---2016 Multi-University Training Contest 1

2016-08-05 22:54 357 查看
转自:http://blog.csdn.net/qingshui23/article/details/51967690#comments

传送门

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 的棋盘,棋盘上有 m 个棋子。如果一个棋子右侧为空,则可以向右移动一格,若有棋子,则可以移到第一个空的位置,两人轮流操作,不能操作者为输,如果先手赢输出YES,否则输出NO。

解题思路:

这是一个博弈的题目,我们看到这个棋盘只有20列,那么我们就可以采用状压的做法,然后跑一下SG就行了,重要的是分析它的后一状态,然后每次有多少可以往后移动的状态,求出来,SG值异或一下就行了。注意的是这个跟行与行之间没有影响。

My Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 21;
int sg[1<<MAXN];
int get_SG(int x)///sg打表。分析他的后继状态
{
int vis[MAXN];///标记数组
memset(vis, 0, sizeof(vis));
for(int i=20; i>=0; i--)
{
if((1<<i) & x)
{
int tmp = x;
for(int j=i-1; j>=0; j--)
{
if(!(x & (1<<j)))
{
tmp ^= ( (1<<i) ^ (1<<j) );
vis[sg[tmp]] = 1;
break;
}
}
}
}
for(int k=0; k<MAXN; k++)
if(!vis[k])
return sg[x] = k;
}
int main()
{
memset(sg, 0, sizeof(sg));
for(int i=0; i<(1<<MAXN); i++)
sg[i] = get_SG(i);
int T;
scanf("%d",&T);
while(T--)
{
int n, m, x, ret = 0;
scanf("%d",&n);
while(n--)
{
scanf("%d",&m);
int ans = 0;///可以移动的总状态数
while(m--)
{
scanf("%d",&x);
ans += (1<<(20-x));
}
ret ^= sg[ans];
}
if(ret)
puts("YES");
else
puts("NO");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐