hdu 5724 SG函数+状压 详解

本文探讨了一种特殊的棋盘游戏,游戏在一个n行20列的棋盘上进行,玩家轮流将棋子向右移动至最近的空位。文章通过SG函数应用,采用状态压缩技术,分析了游戏的策略,确定先手玩家是否能够赢得游戏。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Chess

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3506    Accepted Submission(s): 1448


 

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

题意:

一个棋盘有n行,每行20格子,每行都有一些棋子,两个人轮流进行这个操作:选择某一行一个棋子移动到该行右边最近的一个空的格子。不能移动的人输。问先手是否能赢。

分析:
SG函数的应用,终结点是这一行没有棋子可以走,即0,然后逆推出其他结点的SG函数。每一行的状态看成是一个结点,然后把状态二进制压缩,1表示有棋子,0表示空格。

(貌似数据在  n<=20 以内都可以用状态压缩来进行)

#include<cstdio>
#include<cstring>
using namespace std;
int sg[1<<21],vis[21];//PN点 记录可达状态
int getSG(int x)//在该状态下
{
  memset(vis,0,sizeof(vis));//每次初始化状态
  for(int i=20; i>=0; i--)//逆推
  {
    if(x&(1<<i))//在该状态下  i空格没有棋子
    {
      int t=x;
      for(int j=i-1; j>=0; j--)//寻找i前面的最近空格
        if(!(x&(1<<j)))//j空格有棋子
        {
          t^=(1<<i)^(1<<j);//在t状态下 棋子从j空格移动到i空格 离他最近的空格子
          vis[sg[t]]=1;//该棋子走向空格
          break;
        }
    }
  }
  for(int i=0; i<=20; i++)if(!vis[i])return i;//返回PN值
}
int main()
{
  memset(sg,0,sizeof(sg));//初始化
  for(int i=0; i<(1<<20); i++)//每一个状态的SG
    sg[i]=getSG(i);
  int T;
  scanf("%d",&T);
  while(T--)
  {
    int n,m,x,ans=0;
    scanf("%d",&n);
    for(int i=0; i<n; i++)
    {
      scanf("%d",&m);
      int st=0;
      while(m--)
      {
        scanf("%d",&x);
        st|=1<<(20-x); //是从右边开始的,21这个位置为0
      }
      ans^=sg[st];
    }
    printf("%s\n",ans?"YES":"NO");
  }
  return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值