当前位置:首页 > > 充电吧
[导读]题面:Chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Su

题面:


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  


题意:

    给定一个n*20的棋盘,棋盘上有若干棋子。如果一颗棋子右侧为空,则只可以向右移动一格,若非空,则可以移到第一个空的位置,两人轮流操作,不能操作者为输,问先者是否有必胜策略。


解题:

    比较简单的博弈,通过SG值的计算即可解决问题。将游戏划分为多个子游戏,每个游戏相互独立,视为一行的棋盘,最后将每行的SG值异或即可。SG值的计算是,其后续状态(即操作一步之后达到的状态)的SG值集合中未出现过的最小自然数。棋盘的状态可以用二进制位表示,1代表有棋子,0代表无棋子。枚举每个状态的后继,计算该状态的SG值。


代码:


#include#include#include#include#include#include#include#include#include#include#include#include#define eps 1e-8
using namespace std;
int dp[1100000];
//本地测试,最大值不超过30
bool vis[30];
//寻找后续状态
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;
		//找到一个和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--;
		   //逐次替换连续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));
	//初始化必输态
	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++)
            {
				//构建进制表示状态
				scanf("%d",&tmp);
				status+=(1<<(20-tmp));
			}
			//异或得出结果
			res^=dp[status];
		}
		if(res)
			printf("YESn");
		else
			printf("NOn");
	}
	return 0;
}



本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

纳祥科技推出太阳能+Type-C双充电自行车前灯方案,方案核心模块包含太阳能板、单片机、三极管、3颗LED灯珠与1200mAh电池,通过低功耗单片机与三极管驱动,支持强光/弱光/爆闪3种模式,高流明远射程,适配多种车型

关键字: 方案开发 电子方案 自行车前灯方案 纳祥科技

慕尼黑2025年9月11日 /美通社/ -- 当地时间9月9日,赛力斯动力在德国慕尼黑国际车展期间举办技术发布与交流会,正式在海外市场推出全新一代赛力斯超级增程、高效发动机和新一代分布式电驱动系统,同时与来自全球的汽车产...

关键字: 慕尼黑 分布式 发动机 新能源汽车

慕尼黑2025年9月11日 /美通社/ -- 高端智能电动汽车品牌问界(AITO)在2025年德国国际汽车及智慧出行博览会(IAA MOBILITY)上,正式发布了其最新全球产品阵容——专为中东市场深度本地化打造的AIT...

关键字: AI 智能驾驶 测试 生态系统

舍弗勒首次为中国头部车企大规模生产高压逆变砖 天津工厂一年内完成量产准备,逆变器模块性能参数显著提升 与合作伙伴罗姆半导体共研尖端碳化硅技术,效率更高、性能更优 模块化可扩展设计使逆变砖易于集成,可广泛...

关键字: 逆变 高压 逆变器 集成

舍弗勒以"专注驱动技术的科技公司"为主题亮相IAA MOBILITY 2025(B3馆B40展台) 合并纬湃科技后首次亮相IAA MOBILITY,展示拓展后的汽车产品组合 凭借在软件、...

关键字: 电气 软件 驱动技术 BSP

拉斯维加斯2025年9月11日 /美通社/ -- 在9月8日至11日举办的RE+ 2025展会上,全球综合储能解决方案供应商德赛电池(Desay Battery)全面展示了其创新成果,并宣布与深圳市华宝新能源股份有限公司...

关键字: 电池 电芯 人工智能 锂电

香港2025年 9月12日 /美通社/ -- 全球领先的互联网社区创建者 - 网龙网络控股有限公司 ("网龙"或"本公司",香港交易所股票代码:777)欣然宣布,其子公司My...

关键字: AI 远程控制 控制技术 BSP

慕尼黑2025年9月12日 /美通社/ -- 慕尼黑当地时间9月10日,在2025德国国际汽车及智慧出行博览会(IAA MOBILITY)上,国际独立第三方检测、检验和认证机...

关键字: 测试 慕尼黑 模型 HUBER

上海2025年9月12日 /美通社/ -- 近日,国际独立第三方检测、检验和认证机构德国莱茵TÜV大中华区(简称"TÜV莱茵")为上海...

关键字: 测试 信息安全 安全管理 开关

广州2025年9月12日 /美通社/ -- 9月11日,由国际独立第三方检测、检验和认证机构德国莱茵TÜV大中华区(简称"TÜV莱茵"...

关键字: 数字化 供应链 控制 电子
关闭