博弈的必勝策略?HDU 5724 Chess解析
題面:
Chess
Time Limit: 2000/1000 MS (Java/Others)????Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 450????Accepted Submission(s): 165
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
?
Author
HIT
?
Source
2016 Multi-University Training Contest 1
?
題意:
??? 給定一個(gè)n*20的棋盤,棋盤上有若干棋子。如果一顆棋子右側(cè)為空,則只可以向右移動(dòng)一格,若非空,則可以移到第一個(gè)空的位置,兩人輪流操作,不能操作者為輸,問先者是否有必勝策略。
解題:
??? 比較簡(jiǎn)單的博弈,通過SG值的計(jì)算即可解決問題。將游戲劃分為多個(gè)子游戲,每個(gè)游戲相互獨(dú)立,視為一行的棋盤,最后將每行的SG值異或即可。SG值的計(jì)算是,其后續(xù)狀態(tài)(即操作一步之后達(dá)到的狀態(tài))的SG值集合中未出現(xiàn)過的最小自然數(shù)。棋盤的狀態(tài)可以用二進(jìn)制位表示,1代表有棋子,0代表無棋子。枚舉每個(gè)狀態(tài)的后繼,計(jì)算該狀態(tài)的SG值。
代碼:
#include#include#include#include#include#include#include#include#include#include#include#include#define?eps?1e-8 using?namespace?std; int?dp[1100000]; //本地測(cè)試,最大值不超過30 bool?vis[30]; //尋找后續(xù)狀態(tài) int?dfs(int?x) { //記憶化搜索 if(dp[x]!=-1) return?dp[x]; int?tmp; memset(vis,0,sizeof(vis)); for(int?i=0;i<19;i++) { ????????if((1<x) break; //找到一個(gè)和1緊鄰的0 if(((x&(1<<i))==0)&&(x&(1<<(i+1)))) { ???int?j=i+2; ???????????for(;j<20;j++) ???{ ???if(x&(1<<j)) ???continue; ???else ???break; ???} ???j--; ???//逐次替換連續(xù)1塊中的每一塊 ???for(int?k=i+1;k<=j;k++) ???{ ??????????????tmp=(x-(1<<k)+(1<<i)); ??tmp=dfs(tmp); ??vis[tmp]=1; ???} } } for(int?i=0;;i++) { if(!vis[i]) ????{ dp[x]=i; break; } } return?dp[x]; } int?main() { memset(dp,-1,sizeof(dp)); //初始化必輸態(tài) for(int?i=0;i<=20;i++) dp[(1<<i)-1]=0; for(int?i=1;i<=1100000;i++) { if(dp[i]==-1) dp[i]=dfs(i); } int?t,n,m,status,res,tmp; scanf("%d",&t); while(t--) { scanf("%d",&n); res=0; for(int?i=0;i<n;i++) { status=0; scanf("%d",&m); for(int?j=0;j<m;j++) ????????????{ //構(gòu)建進(jìn)制表示狀態(tài) scanf("%d",&tmp); status+=(1<<(20-tmp)); } //異或得出結(jié)果 res^=dp[status]; } if(res) printf("YESn"); else printf("NOn"); } return?0; }