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 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 cant be placed at one block and no chess can be placed out of the chessboard. When someone cant 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.

Alice和Bob正在一个n × 20的棋盘上玩自定国际象棋。

棋盘上有若干子。每方在本回合只能行一子。如果被移动的子的右邻格为空,则移动之。否则跳过挡路的子,直接移动到他们的右边。一格只能落一子,并且棋子不能移出界。若某人在己方回合无法移动任何子,则输。

Alice总是执先手。Alice与Bob都会使用最佳策略。Alice希望得知自己是否能赢得这场游戏。

 

Input

输入

Multiple test cases.

 

The first line contains an integer T(T100), indicates the number of test cases.

 

For each test case, the first line contains a single integer n(n1000), the number of lines of chessboard.

 

Then n lines, the first integer of ith line is m(m20), indicates the number of chesses on the ith line of the chessboard. Then m integers pj(1pj20) followed, the position of each chess.

多组测试用例。

 

第一行有一个整数T(T100),表示测试用例的数量。

 

对于每个测试用例,第一行有一个整数n(n1000),表示棋子的行数。

 

随后n行,每行第一个数为m(m20),表示此行棋子的数量。接着有m个整数pj(1pj20),表示各个棋子的位置。

 

Output

输出

For each test case, output one line of YESif Alice can win the game, NOotherwise.

对于每个测试用例,如果Alice能赢则输出“YES,否则输出“NO

 

Sample Input - 输入样例

Sample Output - 输出样例

2
1
2 19 20
2
1 19
1 18

NO
YES

 

【题解】

SG定理,百度上解释看着不明觉厉,自己琢磨实现拙劣难成,最后再结合别人的代码似乎自行理解了一下,然而回头看百度上的解释依然看不懂。

 

总结自百度:

这种游戏必定能分出输赢,不是我方输就是对方赢。每种状态都当初是我发先手,最后必输的状态是确定的。即SG[x] = 0,必输。

如果我方的起手状态是SG[x] = 0的上一个状态,则必定 SG[y] = 1,必胜。

输赢某种意义上是不断转化的。

 

由此,必输状态的下一个状态是必胜状态。必胜状态的下一个状态如果不是必输状态,则为若干状态相互组成,这里我个人称作吊打状态(我告诉你,我有X种方式来解决些问题)。

 

总结自代码以及个人作死:

对于单场(一行)游戏来说:

SG = 0,无力回天。

SG = 1,高手躺赢。

SG > 1,花式吊打。

 

因为这道题涉及到多场游戏,所以我们需要区分种类不同的花式吊打,比如花式吊打对手的时候可能是:无情吊打(SG = 2),残忍虐杀(花式吊打SG = 3),笑而不语(SG = 4)…………等等。

 

然后我们从0(必输)开始看看是怎么确定出上一个状态的。

先祖状态 SG = 神马都没有,当前状态 0

先祖状态SG = {0},当前状态 1

(问题来了,如何才能吊打对手?这局我想输就输,想赢就赢。)

先祖状态SG = {0, 1},当前状态 2

(似乎掌握了所有前置先祖状态才能推导出下一个状态,不能解决前面的所有状态,怎么能叫吊打?)

因此当前SG[now] = 先祖SG{abc……}中从0开始的最长连续长度。

先祖SG为空,当前SG0

(其中先祖SG无重复,因为重复出现相当于某种一样上的等价先祖,取谁有一样)

 

个人总结SG定理:连续花式吊打数。

关于总场次的胜负判定:

对于各个场次,我发先手状态下若为:必胜 + 必胜

最终结果则为 必负。

因此:

必胜 + 必胜 必负。

必负 + 必负 必负。

必胜 + 必负 必胜。

 

此处直接转为疑惑运算(你要判断奇偶我也不拦你……):

1 ^ 1 = 0;

0 ^ 0 = 0;

1 ^ 0 = 0;

 

所以真正能决定某种状态(SG)变化的只有相同的状态(SG),结果的运算也就是见招拆招,所以就是不断地异或(真的,你要算奇偶我不会拦你的)

 

最终结果:SG[a1] ^ SG[a2] ^ ………… ^ SG[an]

对应单场游戏的结果:

0,默哀

1,躺赢

大于1,吊打

【代码 C++

 1 #include <cstdio>
 2 #include <cstring>
 3 int SG[(1 << 20) + 5];
 4 void rdy(){
 5     int i, j, last, isUS[20];
 6     for (i = 1; i < (1 << 20); ++i){
 7         memset(isUS, 0, sizeof(isUS)); last = -1;
 8         for (j = 0; j < 20; ++j){
 9             if (i&(1 << j)){
10                 if (~last) isUS[SG[i ^ (1 << j) ^ (1 << last)]] = 1;
11             }
12             else last = j;
13         }
14         for (j = 0; isUS[j]; ++j);
15         SG[i] = j;
16     }
17 }
18 int main(){
19     rdy();
20     int t, n, m, p, now, s, w;
21     for (scanf("%d", &t); t; --t){
22         s = 0;
23         for (scanf("%d", &n); n; --n){
24             now = 0;
25             for (scanf("%d", &m); m; --m){
26                 scanf("%d", &p); now |= 1 << 20 - p;
27             }
28             s ^= SG[now];
29         }
30         if (s) puts("YES");
31         else puts("NO");
32     }
33     return 0;
34 }

 

原文地址:https://www.cnblogs.com/Simon-X/p/5905960.html