sg值的求解(NIM)
硬币游戏2
挑战程序设计竞赛P315
1堆的情况:
#include<bits/stdc++.h>
int x=,grundy[],k=,A[]={,},n=;
using namespace std;
int main(){
grundy[]=;
for(int i=;i<=;i++){
set<int>s;
for(int j=;j<k;j++){
if(i>=A[j]) s.insert(grundy[i-A[j]]);
}
int g=;
if(s.count(g)!=) g++;
grundy[i]=g;
}
if(grundy[x]) printf("Alice\n");
else printf("Bob\n"); }
n堆的情况:
#include<bits/stdc++.h>
#define MAX_N 1000
#define MAX_K 1000
#define MAX_X 1000
using namespace std;
int N=,K=,X[MAX_N]={,,},A[MAX_K]={,,};
int grundy[MAX_X+];
int main(){
grundy[]=;
int max_x=*max_element(X,X+N); for(int i=;i<=max_x;i++){
set<int>s;
for(int j=;j<K;j++){
if(A[j]<=i) s.insert(grundy[i-A[j]]);
}
int g=;
while(s.count(g)!=) g++;
grundy[i]=g;
} int x=;
for(int i=;i<N;i++) x^=grundy[X[i]]; if(x!=) puts("Alice\n");
else puts("Bob\n");
return ;
}
sg值的求解(NIM)的更多相关文章
- hdu5795 A Simple Nim 求nim求法,打表找sg值规律 给定n堆石子,每堆有若干石子,两个人轮流操作,每次操作可以选择任意一堆取走任意个石子(不可以为空) 或者选择一堆,把它分成三堆,每堆不为空。求先手必胜,还是后手必胜。
/** 题目:A Simple Nim 链接:http://acm.hdu.edu.cn/showproblem.php?pid=5795 题意:给定n堆石子,每堆有若干石子,两个人轮流操作,每次操作 ...
- hdu1356&hdu1944 博弈论的SG值(王道)
S-NimProblem DescriptionArthur and his sister Caroll have been playing a game called Nim for some ti ...
- 【UVA1378】A Funny Stone Game (博弈-求SG值-输出方案)
[题目] Description The funny stone game is coming. There are n piles of stones, numbered with 0, 1, 2, ...
- 题解报告:hdu 1847 Good Luck in CET-4 Everybody!(入门SG值)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1847 Problem Description 大学英语四级考试就要来临了,你是不是在紧张的复习?也许紧 ...
- Nim or not Nim? hdu3032 SG值打表找规律
Nim or not Nim? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- hdu 1536 S-Nim_求sg值模版
题意:给你很n堆石头,k代表你有k种拿法,然后给出没堆石头的数量,求胜负 直接套用模版 找了好久之前写的代码贴上来 #include<iostream> #include<algor ...
- Treblecross 博弈SG值
Treblecross is a two player game where the goal is to get three X in a row on a one-dimensional boar ...
- Nim 游戏、SG 函数、游戏的和
Nim游戏 Nim游戏定义 Nim游戏是组合游戏(Combinatorial Games)的一种,准确来说,属于“Impartial Combinatorial Games”(以下简称ICG).满足以 ...
- 博弈论之Nim
博弈论(一):Nim游戏 重点结论:对于一个Nim游戏的局面(a1,a2,...,an),它是P-position当且仅当a1^a2^...^an=0,其中^表示位异或(xor)运算. Nim游戏是博 ...
随机推荐
- C++中int转string与string转int
#include "stdafx.h" #include "string" #include "iostream" #include &qu ...
- 01-THREE.JS 第一个场景
THREE.JS第一个场景 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...
- HTML中可以连接资源的标签集合
1.<a>标签,href属性指示链接的目标,可以是HTML也可以是内部css样式.<a href="http://www.w3school.com.cn"> ...
- LeetCode OJ:Construct Binary Tree from Preorder and Inorder Traversal(从前序以及中序遍历结果中构造二叉树)
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- linux shell 学习笔记--比较操作
整数比较 -eq 等于,如:if [ "$a" -eq "$b" ] -ne 不等于,如:if [ "$a" -ne "$b&qu ...
- RTP协议全解(H264码流和PS流)
写在前面:RTP的解析,网上找了很多资料,但是都不全,所以我力图整理出一个比较全面的解析, 其中借鉴了很多文章,我都列在了文章最后,在此表示感谢. 互联网的发展离不开大家的无私奉献,我决定从我做起,希 ...
- 元素为指针的vector的使用说明
该程序演示了vector中的元素为指针的时候的对对象的操作. /* 功能说明: 元素为指针的vector的使用说明 实现方式: 使用this成员来显示各个对象的地址. 限制条件或者存在的问题: 无 * ...
- HDU1815 Building roads(二分+2-SAT)
Problem Description Farmer John's farm has N barns, and there are some cows that live in each barn. ...
- Shiro-Session
概述 Shiro提供了完整的企业级会话管理功能,不依赖于底层容器(如web容器tomcat),不管JavaSE还是JavaEE环境都可以使用,提供了会话管理.会话事件监听.会话存储/持久化.容器无关的 ...
- AngularJs出现错误Error: [ng:areq]
1.没有对应的控制器 2.有控制器但是路径没有配对