gitbook-way-to-algorithm
  • Introduction
  • Preface 前言
  • Content 目录
  • MathSymbolTable 数学符号表
  • Chapter-1 BasicKnowledge 第1章 基础知识
    • TimeComplexity 时间复杂度
    • Recursion 递归式
  • Chapter-2 Sort 第2章 排序
    • InsertSort 插入排序
    • BubbleSort 冒泡排序
    • QuickSort 快速排序
    • MergeSort 归并排序
  • Chapter-3 Search 第3章 搜索
    • BinarySearch 二分查找法(折半查找法)
    • AdditionMultiplicationPrinciple 加法乘法原理
    • BruteForce 暴力枚举
    • Recursion 递归
    • BreadthFirstSearch 广度优先搜索
    • BidirectionalBreadthSearch 双向广度搜索
    • AStarSearch A*搜索
    • DancingLink 舞蹈链
  • Chapter-4 DataStructure 第4章 数据结构
    • DisjointSet 并查集
    • PrefixTree(TrieTree) 前缀树
    • LeftistTree(LeftistHeap) 左偏树(左偏堆)
    • SegmentTree 线段树
    • FenwickTree(BinaryIndexedTree) 树状数组
    • BinarySearchTree 二叉查找树
    • AVLTree AVL平衡树
    • RedBlackTree 红黑树
    • SkipList 跳跃表
    • BPlusTree B+树
    • BMinusTree B-树
  • Chapter-5 DynamicProgramming 第5章 动态规划
    • Section-1 LinearDP 第1节 线性动规
      • LongestCommonSubsequence 最长公共子序列
      • LongestIncreasingSubsequence 最长递增子序列
      • BidirectionalSubsequence 双向子序列
      • MaximumContinuousSubsequenceSum 最大连续子序列和
      • LongestPalindromicSubsequence 最长回文子序列
    • Section-2 BagDP 第2节 背包问题
      • 01-Bag 01背包
      • CompleteBag 完全背包
      • TwoDimensionBag 二维背包
    • Section-3 RegionalDP 第3节 区域动规
      • MinimumMergeCost - 最小合并代价
      • UniquePath 唯一路径
      • TrianglePath 三角形路径
    • Section-4 TreeDP 第4节 树形动规
      • MaximumBinaryTree 最大二叉树
      • MaxMultipleTree 最大多叉树
      • MaximumBinaryTreeRadiusSum 最大二叉树和
  • Chapter-6 GraphTheory 第6章 图论
    • Section-1 Traverse 第1节 遍历
      • DepthFirstSearch 深度优先搜索
      • BreadthFirstSearch 广度优先搜索
      • TopologicalSort 拓扑排序
      • EulerCycle 欧拉回路
    • Section-2 MinSpanningTree 第2节 最小生成树
      • Kruskal Kruskal算法
      • Prim Prim算法
    • Section-3 ShortestPath 第3节 最短路径
      • BellmanFord BellmanFord算法
      • Dijkstra Dijkstra算法
      • FloydWarshall FloydWarshall算法
      • DifferentConstraints 差分约束
    • Section-4 StronglyConnectedComponents 第4节 强连通分支
      • Kosaraju Kosaraju算法
      • Tarjan Tarjan算法
      • 2-SAT 2-SAT问题
    • Section-5 NetworkFlow 第5节 网络流
      • EdmondsKarp EdmondsKarp算法(最短路径增广算法)
      • PushRelabel 压入与重标记算法
      • Dinic - Dinic算法
      • MinimumCostFlow - 最小费用流
      • MultipleSourceMultipleSinkMaxflow - 多源点多汇点最大流
    • Section-6 BinaryMatch 第6节 二分匹配
      • Hungarian 匈牙利算法
      • HopcroftKarp Hopcroft-Karp算法
      • MatchToMaxflow 二分匹配转化为最大流
      • KuhnMunkres Kuhn-Munkres算法
      • Introduction-Domination,Independent,Covering,Clique 介绍支配集、独立集、覆盖集和团
      • WeightedCoveringAndIndependentSet 最小点权覆盖和最大点权独立集
      • MinimumDisjointPathCovering 最小不相交路径覆盖
      • MinimumJointPathCovering 最小可相交路径覆盖
      • Coloring 染色问题
  • Chapter-7 CombinatorialMathematics 第7章 组合数学
    • FullPermutation 全排列
    • Combination 组合
    • Permutation 排列
    • PermutationGroup 置换群
  • Chapter-8 NumberTheory 第8章 数论
    • PrimeSieve 素数筛法
    • GreatestCommonDivisor 最大公约数
    • Euclid 欧几里得算法
    • ExtendedEuclid 扩展欧几里得算法
    • ChineseRemainerTheorem 中国剩余定理
    • ModularExponentiation 模幂运算
  • Chapter-9 LinearAlgebra 第9章 线性代数
    • Section-1 Matrix 第1节 矩阵
      • Strassen Strassen算法
      • GaussElimination 高斯消元法
      • LUP LUP分解
      • InverseMatrix 矩阵求逆
    • Section-2 LinearProgramming 第2节 线性规划
      • Simplex 单纯形算法
      • Dinkelback Dinkelback算法
  • Chapter-10 AnalyticGeometry 第10章 解析几何
    • Section-1 Polygon 第1节 多边形
      • Cross 向量叉积
      • SegmentIntersection 线段相交
      • Sweeping 扫除算法
      • ConvexPolygonArea 凸多边形面积
      • ConvexPolygonGravityCenter 凸多边形重心
      • NearestNeighbor 最近点对
    • Section-2 ConvexHull 第2节 凸包
      • GrahamScan Graham扫描算法
      • QuickHull 快速凸包算法
      • RotatingCalipers 旋转卡壳
  • Chapter-11 PatternMatch 第11章 文本匹配
    • SimpleMatch 简单匹配
    • AhoCorasickAutomata AC自动机
    • KnuthMorrisPratt KMP匹配算法
    • RabinKarp RabinKarp算法
    • BoyerMoore BoyerMoore算法
  • Chapter-12 GameTheory 第12章 博弈论
    • BashGame 巴什博弈
    • WythoffGame 威佐夫博弈
    • NimGame 尼姆博弈
Powered by GitBook
On this page
  • 问题
  • 解法
  • Nim Sum
  • 源码
  • 测试
  1. Chapter-12 GameTheory 第12章 博弈论

NimGame 尼姆博弈

问题

AAA和BBB两人轮流从nnn堆物品中取出一些物品,nnn堆物品的数量分别为[s1,s2,s3…sn][ s_{1}, s_{2}, s_{3} \dots s_{n} ][s1​,s2​,s3​…sn​](所有物品数量都是正整数)。

每人每次从一堆物品中至少取111个,多则不限,最后取光所有物品的人获胜。

给定nnn和[s1,s2,s3…sn][ s_{1}, s_{2}, s_{3} \dots s_{n} ][s1​,s2​,s3​…sn​],当我方先手,我方和对方都是高手(在能赢的情况下一定能赢),求我方是否能赢。

解法

(1)(1)(1) 当我方面临[0,7,0][0, 7, 0][0,7,0]局势(有111堆物品)时,我方必赢,因为我方可以一次把剩下一组的物品取光;

(2)(2)(2) 当我方面临[0,1,1,0,0][0, 1, 1, 0, 0][0,1,1,0,0]局势(有222堆物品且均剩111个)时,我方必输,因为我方必然留给对方只剩111堆物品的局势;

(3)(3)(3) 当我方面临[0,1,1,1,0][0, 1, 1, 1, 0][0,1,1,1,0]局势(有333堆物品且均剩111个)时,我方必赢,因为我方必然留给对方只剩222堆物品且均剩111个的局势;

(4)(4)(4) 当我方面临[3,4,5][3, 4, 5][3,4,5]局势时,暂时无法看出我方是否必赢;

⋯\cdots⋯

本问题背后的数学模型叫NimSumNim SumNimSum,堆数组大小的二进制和,上面555个局势可以转化为:

s1=010=0002s2=710=1112s3=010=0002nim1=0⊕7⊕0=1\begin{matrix} s_{1} = 0_{10} = 000_{2} \\ s_{2} = 7_{10} = 111_{2} \\ s_{3} = 0_{10} = 000_{2} \\ nim_{1} = 0 \oplus 7 \oplus 0 = 1 \end{matrix}s1​=010​=0002​s2​=710​=1112​s3​=010​=0002​nim1​=0⊕7⊕0=1​
s1=010=0002s2=110=0012s3=110=0012s4=010=0002s5=010=0002nim2=0⊕1⊕1⊕0⊕0=0\begin{matrix} s_{1} = 0_{10} = 000_{2} \\ s_{2} = 1_{10} = 001_{2} \\ s_{3} = 1_{10} = 001_{2} \\ s_{4} = 0_{10} = 000_{2} \\ s_{5} = 0_{10} = 000_{2} \\ nim_{2} = 0 \oplus 1 \oplus 1 \oplus 0 \oplus 0 = 0 \end{matrix}s1​=010​=0002​s2​=110​=0012​s3​=110​=0012​s4​=010​=0002​s5​=010​=0002​nim2​=0⊕1⊕1⊕0⊕0=0​
s1=010=0002s2=110=0012s3=110=0012s4=110=0012s5=010=0002nim3=0⊕1⊕1⊕1⊕0=1\begin{matrix} s_{1} = 0_{10} = 000_{2} \\ s_{2} = 1_{10} = 001_{2} \\ s_{3} = 1_{10} = 001_{2} \\ s_{4} = 1_{10} = 001_{2} \\ s_{5} = 0_{10} = 000_{2} \\ nim_{3} = 0 \oplus 1 \oplus 1 \oplus 1 \oplus 0 = 1 \end{matrix}s1​=010​=0002​s2​=110​=0012​s3​=110​=0012​s4​=110​=0012​s5​=010​=0002​nim3​=0⊕1⊕1⊕1⊕0=1​
s1=310=0112s2=410=1002s3=510=1012nim4=3⊕4⊕5=2\begin{matrix} s_{1} = 3_{10} = 011_{2} \\ s_{2} = 4_{10} = 100_{2} \\ s_{3} = 5_{10} = 101_{2} \\ nim_{4} = 3 \oplus 4 \oplus 5 = 2 \end{matrix}s1​=310​=0112​s2​=410​=1002​s3​=510​=1012​nim4​=3⊕4⊕5=2​

可以看出,当我方面临⊕i=1nsi=s1⊕s2⊕⋯⊕sn≠0\oplus_{i=1}^{n} s_{i} = s_{1} \oplus s_{2} \oplus \cdots \oplus s_{n} \ne 0⊕i=1n​si​=s1​⊕s2​⊕⋯⊕sn​=0局势时必赢,否则必输。

该算法时间复杂度为O(n)O(n)O(n)。

Nim Sum

源码

测试

PreviousWythoffGame 威佐夫博弈

Last updated 6 years ago

http://www.math.ucla.edu/~radko/circles/lib/data/Handout-141-156.pdf
https://plus.maths.org/content/play-win-nim
http://samidavies.com/post/2016/03/09/games-intro.html
https://paradise.caltech.edu/ist4/lectures/Bouton1901.pdf
https://pdfs.semanticscholar.org/8ac7/c5d8d56847daafa73ad85ae2ad6f47149096.pdf
https://www.researchgate.net/publication/220343088_The_game_of_End-Nim
NimGame.h
NimGame.cpp
NimGameTest.cpp