浅谈公平组合游戏

· · 个人记录

定义

如果一个游戏满足:

  • 游戏有两个人参与,二者轮流做出决策,双方均知道游戏的完整信息
  • 任意一个游戏者在某一确定状态可以作出的决策集合只与当前的状态有关,而与游戏者无关
  • 游戏中的同一个状态不可能多次抵达,游戏以玩家无法行动为结束,且游戏一定会在有限步后以非平局结束
  • 双方可采取的行动及胜利目标都相同
  • 走最后一步者为胜

那么我们称这个游戏为「公平组合游戏」

Nim 游戏

之后我们将会用这个问题引入,讨论公平组合游戏

建图法

像刚才说的一样,我们以 Nim 游戏举例。设状态 S=(a_1,a_2,\cdots,a_n) 表示 N 堆石子的个数分别为 a_1,a_2,\cdots,a_n。如果 S_1 走一步后能变为 S_2,就从 S_1 向 S_2 连一条有向边。

定义 P-position 为先手必败的状态,N-position 为先手必胜的状态,那么我们有以下结论

利用以上规则,我们可以使用记忆化搜索达到 O(\prod a_i) 的时间复杂度

例题

CF917B MADMAX

\mathrm{SG} 函数 与 \mathrm{SG} 定理

注:SG 为 Sprague-Grundy 的缩写

只保证以下内容对「公平组合游戏」适用

\mathrm{SG} 函数

定义

\mathrm{SG}(S)=\operatorname{mex}\{\mathrm{SG}(T)\mid S \to T\}

其中

\mathrm{mex}\{S\}=\min\{x\mid x \in \mathbf{N} \wedge x\notin S\}

可以发现

正确性显然

\mathrm{SG} 定理

若能将状态 S 划分成相互独立的状态 S_1,S_2,\cdots,S_n(即每次能且仅能改变一个子状态),则称 S=S_1+S_2+\cdots+S_n,那么

\mathrm{SG}\left(\sum_{i=1}^{n}S_i\right)=\mathrm{SG}(S_1)\oplus \mathrm{SG}(S_2) \oplus \cdots \oplus \mathrm{SG}(S_n)

其中 \oplus 表示异或运算

显然,欲证原命题,只需证

\mathrm{SG}(A+B)=\mathrm{SG}(A) \oplus \mathrm{SG}(B)

考虑到 A,B 中只能选一个走一步,故有

\mathrm{SG}(A+B)=\operatorname{mex}\{\mathrm{SG}(C+B)\mid A \to C\} \cup \{\mathrm{SG}(A+D)\mid B \to D\}

此时用拓扑序进行数学归纳法,这样只会向归纳过的方向跳。归纳假设是 \mathrm{SG}(A+B)=\mathrm{SG}(A) \oplus \mathrm{SG}(B),初始条件 A,B 均为终局状态,那么

\mathrm{SG}(A+B)=\operatorname{mex}\{\mathrm{SG}(C)\oplus \mathrm{SG}(B)\mid A\to C\} \cup \{\mathrm{SG}(A)\oplus \mathrm{SG}(D)\mid B \to D\}

故所证即为

\mathrm{SG}(A) \oplus \mathrm{SG}(B) \notin \{\mathrm{SG}(C)\oplus \mathrm{SG}(B)\mid A\to C\} \cup \{\mathrm{SG}(A)\oplus \mathrm{SG}(D)\mid B \to D\} \forall 0\leq x<\mathrm{SG}(A) \oplus \mathrm{SG}(B),\ x \in \{\mathrm{SG}(C)\oplus \mathrm{SG}(B)\mid A\to C\} \cup \{\mathrm{SG}(A)\oplus \mathrm{SG}(D)\mid B \to D\}

证明一式:显然 \mathrm{SG}(C) \neq \mathrm{SG}(A),\ \mathrm{SG}(D) \neq \mathrm{SG}(B)(因为 \operatorname{mex}),由异或运算的性质,两集合均不含 \mathrm{SG}(A) \oplus \mathrm{SG}(B)

证明二式:设 t=\mathrm{SG}(A)\oplus \mathrm{SG}(B)\oplus x,容易发现 \mathrm{SG}(A) 与 \mathrm{SG}(B) 异或 t 后必然有一个变小(因为 t 的最高位必然与其中一个的最高位相同),所以不妨设 t \oplus \mathrm{SG}(A)=x \oplus \mathrm{SG}(B)<\mathrm{SG}(A),可以发现,必然存在 \mathrm{SG}(C)=x \oplus \mathrm{SG}(B)(由 \mathrm{SG} 函数定义得)

Nim 游戏

Nim 游戏的必胜策略(Bouton 定理)

注:以下各变量意义同上

由上文的 \mathrm{SG} 定理可以得到 Nim 游戏的 \mathrm{SG} 函数

\mathrm{SG}(S)=a_1 \oplus a_2 \oplus \cdots \oplus a_n

板子:取火柴游戏

阶梯 Nim 游戏

建议读者先自行思考几分钟,提示:按层数的奇偶性分类

可以发现

例题

高手过招

比较巧妙的阶梯 Nim 应用,我的题解

其它

欧几里德的游戏

欧几里德的游戏

不妨设 M>N,设 M=kN+t\ (k \in \mathbf{N_+},\ 0 \leq t \leq N-1),那么

递归求解即可

参考

OI wiki:博弈论