HDU 5724 Chess(博弈论)
【题目链接】 http://acm.hdu.edu.cn/showproblem.php?pid=5724
【题目大意】
给出一个n行,每行有20格的棋盘,棋盘上有一些棋子,每次操作可以选择其中一个棋子,将其移至最左端的空位,两个人轮流操作,无法操作者输,判断游戏胜负。
【题解】
首先对于单行20格的游戏,这是一个NIM游戏,将20格的情况状态压缩,对于每种情况递归求其mex集合,计算其sg值,sg值为0的状态为必败态。
而对于可以拆分为多组NIM游戏的游戏,其sg值为拆分出的多组游戏的sg值的异或和。
预处理所有状态的sg值,对于每种读入的棋盘情况,直接求出解即可。
【代码】
#include <cstdio>
#include <cstring>
using namespace std;
const int N=1<<20;
int sg[N],T,n,m,x;
int dfs(int x){
if(sg[x]!=-1)return sg[x];
int mex[50]={0},pos=-1;
for(int i=0;i<20;i++){
if((x&(1<<i))==0)pos=i;
else if(pos!=-1)mex[dfs(x^(1<<i)|(1<<pos))]=1;
}for(int i=0;i<N;i++)if(!mex[i])return sg[x]=i;
}
void init(){
memset(sg,-1,sizeof(sg));
for(int i=0;i<=20;i++)sg[(1<<i)-1]=0;
for(int i=1;i<N;i++)dfs(i);
}
int main(){
init();
scanf("%d",&T);
while(T--){
int SG=0;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&m);
int tmp=0;
for(int j=1;j<=m;j++)scanf("%d",&x),tmp|=(1<<(20-x));
SG^=sg[tmp];
}if(SG)puts("YES");
else puts("NO");
}return 0;
}
HDU 5724 Chess(博弈论)的更多相关文章
- HDU 5724 Chess(国际象棋)
HDU 5724 Chess(国际象棋) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- HDU 5724 Chess (sg函数)
Chess 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5724 Description Alice and Bob are playing a s ...
- HDU 5724 Chess(SG函数+状态压缩)
http://acm.split.hdu.edu.cn/showproblem.php?pid=5724 题意: 现在有一个n*20的棋盘,上面有一些棋子,双方每次可以选择一个棋子把它移动到其右边第一 ...
- hdu 5724 Chess 博弈sg+状态压缩
Chess Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem De ...
- HDU 5724 Chess(SG函数)
Chess Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submi ...
- HDU 5724 Chess (状态压缩sg函数博弈) 2016杭电多校联合第一场
题目:传送门. 题意:有n行,每行最多20个棋子,对于一个棋子来说,如果他右面没有棋子,可以移动到他右面:如果有棋子,就跳过这些棋子移动到后面的空格,不能移动的人输. 题解:状态压缩博弈,对于一行2^ ...
- HDU 5724 - Chess
题意: 一个n行20列的棋盘. 每一行有若干个棋子. 两人轮流操作, 每人每次可以将一个棋子向右移动一个位置, 如果它右边有一个棋子, 就跳过这个棋子, 如果有若干个棋子, 就将这若干个 ...
- hdu 5724 Chess 博弈
题目链接 一个n行20列的棋盘. 每一行有若干个棋子. 两人轮流操作, 每人每次可以将一个棋子向右移动一个位置, 如果它右边有一个棋子, 就跳过这个棋子, 如果有若干个棋子, 就将这若干个都跳过. 但 ...
- HDU 5724:Chess(博弈 + 状压)
http://acm.hdu.edu.cn/showproblem.php?pid=5724 Chess Problem Description Alice and Bob are playing ...
随机推荐
- leetcode Combination Sum II python
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- this的用法this.name=name 这个什么意思
public Employee(string name, string alias){ // Use this to qualify the fields, name and alias: this. ...
- [LeetCode]题解(python):154-Find Minimum in Rotated Sorted Array II
题目来源: https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ 题意分析: 给定一个有重复的翻转的数组,找到最 ...
- 1.org.hibernate.MappingException: No Dialect mapping for JDBC type: -9
org.hibernate.MappingException: No Dialect mapping for JDBC type: -9 原因:Hibernate框架的方言(Dialect )没有数据 ...
- spring的作用及优势---第一个spring示例
Spring 的作用及优势 * Spring 用于整合,好处是解耦. 解耦,可以降低组件不组件乊间的关联,改善程序结构,便于系统的维护和扩展. 我们在使用 Spring 框架时,主要是使用 Spri ...
- discuz函数dgmdate
function dgmdate($timestamp, $format = 'dt', $timeoffset = '9999', $uformat = '') { global $_G; $for ...
- 浅谈Web Api配合SignalR的跨域支持
最近接手的一个项目中,涉及到一个简单的消息模块,由于之前有简单了解过SignalR,所以打算尝试着摸索摸索~! 首先,通过Nuget管理器添加Microsoft ASP.NET SignalR引用~目 ...
- 微信开发-Jssdk调用分享实例
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.IO ...
- Java中Lambda表达式的使用
简介(译者注:虽然看着很先进,其实Lambda表达式的本质只是一个"语法糖",由编译器推断并帮你转换包装为常规的代码,因此你可以使用更少的代码来实现同样的功能.本人建议不要乱用,因 ...
- C#控件大小随窗体大小等比例变化
相信很多博友在开发初次接触学习C# winForm时,当窗体大小变化时,窗体内的控件并没有随着窗体的变化而变化,最近因为一个项目工程的原因,也需要解决这个问题.通过查阅和学习,这个问题得到了解决,或许 ...