hdu5724——chess(博弈)(状态压缩dp+sg函数)

本文介绍了一个特殊的棋盘游戏,通过组合博弈理论中的sg函数和状态压缩动态规划解决该问题。游戏中,玩家在一个n行20列的棋盘上移动棋子,目标是在对手无法移动时获胜。文章提供了详细的算法实现过程。

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.

大意:题目大意:n行20列的棋盘,对于每行,如果当前棋子右边没棋子,那可以直接放到右边,如果有就跳过放到其后面的第一个空位子,A先操作,最后谁无法操作则输,给定每行棋子状态,问先手是否必胜。

题解:组合博弈问题,应用sg函数+状态压缩dp可解。
sg函数可看白书的组合游戏部分。

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = (1 << 20) + 10;
int SG[maxn];
int cnt = 0;
int n,m;   
int dfs(int k)
{
    if(SG[k]!=-1) return SG[k];
    int vis[30];
    memset(vis,0,sizeof(vis));
    for(int i=0;i<19;i++){
        int temp = k;
        if(k & (1 << i) == 0) continue;
        if((k & (1 << i))) {
            for(int j=i+1;j<=19;j++){
                if((k & (1 << j)) == 0) {
                    temp = k;
                    temp ^= (1 << i);
                    temp ^= (1 << j);
                    vis[dfs(temp)] = 1; 
                    break;
                }
            }
        }
    }
    for(int i = 0;i < 29;i++) 
      if(vis[i]==0) {
        SG[k] = i;
        return i;
      }
}
int main()
{
    freopen("B.in","r",stdin);
    freopen("B.out","w",stdout); 
    memset(SG,-1,sizeof(SG));
    int temp = 0;
    for(int i=19;i>=0;i--){
        temp += (1 << i);
        SG[temp] = 0;
    }
    int kase;scanf("%d",&kase);
    while(kase--){
        int temp = 0,a,ans = 0;
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            temp = 0;
            scanf("%d",&m);
            for(int j=1;j<=m;j++){
                scanf("%d",&a);
                temp ^= (1 << (a-1));
            }
            int flag = dfs(temp);
            ans ^= SG[temp];
        }
        if(ans != 0) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值