您的位置:首页 > 编程语言 > C语言/C++

2016多校训练#1 1002 组合博弈

2016-08-06 18:19 465 查看
              对于一开始接触博弈论的同学来说,这道题的思路略难,但是如果想到了把1*20的棋盘想象成一个20位的二进制数,然后通过sg函数预处理得到每一个二进制数相应的sg'值,最后直接用每一行的sg值相亦或即可。注意mex数组,也就是vis数组可以开得大一些,在没有确定sg'函数值范围的情况下:

             


Chess

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 1715    Accepted Submission(s): 748


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

 

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<vector>
#include<map>
#include<algorithm>
#include<queue>
#include<stack>//博弈论,求sg函数,难题
using namespace std;
int sg[1<<21];
int bit[40];
int vis[100];//vis数组尽量开大,防止数组越界
//棋子在走的过程中,二进制的数是递减的,因此可以利用之前数组存的值
//来降低时间复杂度~
int g(int x)//sg函数
{
if(sg[x] != -1)
return sg[x];
memset(vis, 0, sizeof(vis));
int cnt = 0;
int tt = x;
while(x)
{
bit[++cnt] = x%2;
x /= 2;
}
int pos = -1;
for(int i = 1; i <= cnt; i++)
{
if(bit[i] == 1 && pos != -1)
vis[sg[tt+(1<<(pos-1))-(1<<(i-1))]] = 1;//注意细节。。
else if(bit[i] == 0)
pos = i;//求最左边有效的0的位置
}
for(int i = 0; i < 100; i++)//记住,这里要从0开始!!
if(!vis[i])
return i;
}

void init()
{
sg[1] = sg[0] = 0;
for(int i = 2; i < (1<<20); i++)
sg[i] = g(i);
}

int main()
{
int t;
memset(sg, -1, sizeof(sg));
init();
scanf("%d", &t);
while(t--)
{
int n, ans = 0;
scanf("%d", &n);
for(int i = 1; i <= n; i++)
{
int m, sum = 0;
scanf("%d", &m);
while(m--)
{
int a;
scanf("%d", &a);
sum += (1<<(20-a));//把棋盘看成二进制数
}
ans ^= sg[sum];
}
if(ans == 0)
printf("NO\n");
else
printf("YES\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息