# 尼姆

**尼姆**（Nim）是一种数学策略游戏，两名玩家轮流从若干堆石子中取子，每步从单独一堆中取走任意正数颗石子，取走最后一颗石子者获胜（在米泽尔玩法中则相反，以取走最后一颗者为负）。<sup>[1](https://mathworld.wolfram.com/Nim.html)</sup> 它是一种无偏博弈，即双方在任何局面下可走的着法完全相同；自1901年起，它就有了完整的数学解法。<sup>[2](https://www.combinatorial-game-theory.com/essays/nim-and-the-nim-sum/)</sup><sup> • </sup><sup>[3](https://open.bu.edu/bitstreams/5163fe14-5364-48f0-8bf5-28266e42e7f9/download)</sup>

| 关键事实 | 详情 |
|---|---|
| 器材与着法 | 一堆或多堆石子；一步棋从单独一堆中取走全部或部分石子。<sup>[1](https://mathworld.wolfram.com/Nim.html)</sup> |
| 正常玩法胜者 | 取走最后一颗石子者获胜。<sup>[1](https://mathworld.wolfram.com/Nim.html)</sup> |
| 完整解法 | 查尔斯·L·布顿（Charles L. Bouton），《数学年刊》，1901年，写于他在哈佛任职期间。<sup>[2](https://www.combinatorial-game-theory.com/essays/nim-and-the-nim-sum/)</sup> |
| 胜负判据 | 当尼姆和——各堆大小的按位异或——为零时，该局面恰为轮到走子者的败局。<sup>[2](https://www.combinatorial-game-theory.com/essays/nim-and-the-nim-sum/)</sup> |
| 米泽尔规则 | 按普通尼姆的走法进行，直到除一堆之外其余各堆大小均为1，然后留下奇数个大小为1的堆。<sup>[2](https://www.combinatorial-game-theory.com/essays/nim-and-the-nim-sum/)</sup> |
| 理论地位 | 每一个有限二人无偏博弈都等价于单独一堆尼姆（斯普拉格1935，格伦迪1939）。<sup>[4](https://boardgamegeek.com/boardgame/11753/nim)</sup><sup> • </sup><sup>[2](https://www.combinatorial-game-theory.com/essays/nim-and-the-nim-sum/)</sup> |
| 欧洲记载 | 自16世纪初即为欧洲所知。<sup>[5](https://iasl.uni-muenchen.de/links/GCA-VII.1e.html)</sup> |

## 规则

石子分成若干堆摆放。轮到时，玩家选定单独一堆，从中取走至少一颗石子，也可以整堆取空。双方轮流走子；按正常玩法规则，取走最后一颗石子者获胜，因为此后对手已无子可取。<sup>[2](https://www.combinatorial-game-theory.com/essays/nim-and-the-nim-sum/)</sup> 这游戏古老得无人知晓其来历，而自1901年起它已被完全解出。<sup>[2](https://www.combinatorial-game-theory.com/essays/nim-and-the-nim-sum/)</sup>

这游戏是无偏的：双方可走的着法完全相同。正因如此，单用一个数就足以描述局面的价值。<sup>[2](https://www.combinatorial-game-theory.com/essays/nim-and-the-nim-sum/)</sup>

## 尼姆和与必胜策略

把每堆的大小写成二进制，按列相加而不进位，等价于取按位异或；所得结果称为尼姆和。<sup>[2](https://www.combinatorial-game-theory.com/essays/nim-and-the-nim-sum/)</sup> 若尼姆和为零，则该局面是轮到走子者的败局。若不为零，则轮到走子者必胜，且存在一步使它变为零。<sup>[2](https://www.combinatorial-game-theory.com/essays/nim-and-the-nim-sum/)</sup> 例如，三堆分别为3、5、7，

\[ 3 \oplus 5 \oplus 7 = 011 \oplus 101 \oplus 111 = 001 = 1, \]

所以先手获胜，取胜的一步是把7那一堆减到6，因为 \( 3 \oplus 5 = 6 \)。<sup>[2](https://www.combinatorial-game-theory.com/essays/nim-and-the-nim-sum/)</sup>

证明分两半。从平衡局面（尼姆和为零）出发，任何一步都会破坏平衡：一步只把某一堆由 \( a \) 变为 \( b \)，且 \( b < a \)，尼姆和便从0变为 \( a \oplus b \)，不为零。从尼姆和 \( s \neq 0 \) 的不平衡局面出发，总有一步能恢复平衡：取一堆 \( a \)，它在 \( s \) 的最高位上有1，把它换成 \( b = a \oplus s \)，\( b \) 更小，因而是合法的削减。<sup>[2](https://www.combinatorial-game-theory.com/essays/nim-and-the-nim-sum/)</sup>

**两堆是每个玩家都会重新发现的特殊情形。** 设有 \( a \)、\( b \) 两堆，尼姆和为零恰在 \( a = b \) 之时，故两堆相等时轮到走子者必败，而取胜策略根本不需要算术：照抄。对手从一堆取走多少，就从另一堆取走多少。<sup>[2](https://www.combinatorial-game-theory.com/essays/nim-and-the-nim-sum/)</sup> 只有当局面从两堆变成三堆时，异或才变得不可或缺，因为三堆时已没有可以模仿的配对结构。<sup>[2](https://www.combinatorial-game-theory.com/essays/nim-and-the-nim-sum/)</sup>

更简单的统计量都不管用。石子总数不管用，因为1、2、3三堆与单独一堆6总数都是六，但前者是后手胜，后者先手一步即胜。堆数也不管用，因为1、1是平衡的，1、2则不是。<sup>[2](https://www.combinatorial-game-theory.com/essays/nim-and-the-nim-sum/)</sup>

## 尼姆对博弈论的意义

尼姆可分解为彼此独立的子博弈：每一堆都是一个独立的游戏，而每一步只影响一堆。大小为 \( n \) 的单堆价值为 \( *n \)，称为尼姆数，其算术为 \( *a + *b = *(a \oplus b) \)，且 \( *a + *a = 0 \)。每个尼姆数都是自己的相反数，这正是镜像策略在成对堆上奏效的原因。<sup>[2](https://www.combinatorial-game-theory.com/essays/nim-and-the-nim-sum/)</sup>

正是这一结构得以推广。1935年罗兰·斯普拉格（Roland Sprague）证明每个无偏博弈都等价于单独一堆尼姆，1939年帕特里克·迈克尔·格伦迪（Patrick Michael Grundy）独立证明了同一结论；自此，布顿的论文不再只是关于一种室内游戏，而成为一般理论的基础情形。<sup>[2](https://www.combinatorial-game-theory.com/essays/nim-and-the-nim-sum/)</sup> 在正常玩法下，有限二人无偏博弈的每个局面都等价于某一堆尼姆，不同局面可以有相同的堆大小；取胜着法依相应的尼姆值而定。<sup>[4](https://boardgamegeek.com/boardgame/11753/nim)</sup>

这一判据也标出了理论的边界。说“从平衡局面出发任何一步都会破坏平衡”的那一半，是关于异或的算术，对任何每次只改动一堆的游戏都成立。而不平衡局面是否存在恢复平衡的一步，则随规则而变；在诸如每步至多取三颗、每堆不得取空、或只许从一端取子之类的限制下，所需的那一步可能并不合法。<sup>[2](https://www.combinatorial-game-theory.com/essays/nim-and-the-nim-sum/)</sup>

尼姆在计算上也容易，而一些相关的游戏则不然。在图中绘出的每一种棋盘尺寸下，它的博弈树复杂度一栏都保持在三步；而多米诺博弈（Domineering）的第一列则大约每换一种棋盘就增长十倍，因为骨牌之间彼此作用，棋盘直到对局后段才分裂成独立的部分。<sup>[6](https://www.combinatorial-game-theory.com/essays/nim-is-easy/)</sup>

## 米泽尔尼姆

把规则改成取走最后一颗石子者输，尼姆仍然可解。在米泽尔规则下，游戏按普通尼姆进行，直到只有一堆含有超过一个物件；此时该玩家把这一大堆全部取空，或只留一个物件在其中，取哪种做法取决于哪种能使单物件堆的数目为奇数。若各堆的大小都已为一，则留下奇数个即胜。<sup>[2](https://www.combinatorial-game-theory.com/essays/nim-and-the-nim-sum/)</sup>

两种规则只在一个狭窄的范围内有分歧。在石子总数不超过十四、且没有一堆大于六的387个尼姆局面中，有十四个局面使两种规则给出不同结果，而它们全都是各堆只含一颗石子的局面。只要还有一堆为二或更多，米泽尔尼姆与普通尼姆就指向同一位胜者。<sup>[2](https://www.combinatorial-game-theory.com/essays/nim-and-the-nim-sum/)</sup> 米泽尔形式正是酒吧里的标准玩法，恰恰因为对随便玩玩的玩家来说，取走最后一个物件感觉像是输了。<sup>[7](https://www.combinatorial-game-theory.com/essays/misere-play/)</sup>

米泽尔尼姆的可解性只是一个特例，没有有用的推广：米泽尔玩法的一般理论比正常玩法理论难得多，因为使正常玩法易于处理的那种等价关系在这里根本失效。<sup>[2](https://www.combinatorial-game-theory.com/essays/nim-and-the-nim-sum/)</sup>

## 历史与机器

像尼姆这样的取石游戏被认为非常古老，而这一游戏自16世纪初就为欧洲所知。<sup>[8](https://puzzlebyrinth.com/en/articles/nim-1901)</sup><sup> • </sup><sup>[5](https://iasl.uni-muenchen.de/links/GCA-VII.1e.html)</sup> 布顿1901年的论文给了这游戏名字、二进制判据和证明，通篇以二进制运算，这在1901年是不同寻常的选择。<sup>[2](https://www.combinatorial-game-theory.com/essays/nim-and-the-nim-sum/)</sup>

完整的解法影响了人们接受这游戏的方式。一项关于早期丹麦计算的研究记载，1901年对尼姆的一项数学分析“杀死了这个游戏”，因为结果很容易预测；也记载了丹麦发明家[皮特·海因](https://www.edgepedia.org/zh/piet-hein)（Piet Hein）想造出一种能躲开这类分析的游戏的心愿；该研究还报告了尼姆比（Nimbi）——1962至1963年间在丹麦公司 Regnecentralen 实现的一个早期电脑游戏，其动机是让计算机与人下棋。<sup>[9](https://researcher.itu.dk/p/en/research-outputs/an-early-danish-computer-game-the-case-of-nim-piet-hein-and-regne)</sup> 电动尼姆机也出于类似的实用理由被造出来：规则简短、取胜方法明确、棋盘可以用灯泡显示。<sup>[8](https://puzzlebyrinth.com/en/articles/nim-1901)</sup>

## 变体

这一游戏衍生出一个庞大的变体家族。在摩尔尼姆 \( (n, \leq k) \) 中，一步至多可削减 \( k \) 堆；在精确尼姆 \( (n, = k) \) 中，一步必须恰好削减 \( k \) 堆。<sup>[10](https://arxiv.org/abs/2311.18772)</sup> 扑克尼姆增加一条规则：每人有一份私人储备，可从中把石子放回某堆；胜负仍由完全相同的尼姆和决定。<sup>[2](https://www.combinatorial-game-theory.com/essays/nim-and-the-nim-sum/)</sup> 建造尼姆是与九子棋同类的两阶段游戏：玩家先轮流把棋子放到初始为空的堆上，而开始尼姆阶段的那位玩家希望最终各堆高度的尼姆和不等于0。<sup>[11](https://arxiv.org/abs/1502.04068)</sup> 减法游戏——一步只能取走某些数量，如完全平方数——也用同样的尼姆值工具来分析。<sup>[12](https://arxiv.org/abs/1804.06515)</sup>

在线实现让玩家可以与完美对手对弈，在正常规则与米泽尔规则之间切换，并清楚看到两种游戏的分歧所在。<sup>[13](https://absurdtools.com/nim-game/)</sup>

## References

1. [Nim, Wolfram MathWorld](https://mathworld.wolfram.com/Nim.html)
2. [Nim, and the nim-sum, Combinatorial Game Theory](https://www.combinatorial-game-theory.com/essays/nim-and-the-nim-sum/)
3. [Nim is an ancient game with a complete mathematical solution since 1901, Boston University open repository](https://open.bu.edu/bitstreams/5163fe14-5364-48f0-8bf5-28266e42e7f9/download)
4. [Nim, BoardGameGeek](https://boardgamegeek.com/boardgame/11753/nim)
5. [History of Computer Art VII.1: Computer and Video Games, IASLonline](https://iasl.uni-muenchen.de/links/GCA-VII.1e.html)
6. [The game with the shortest rule is the hard one, Combinatorial Game Theory](https://www.combinatorial-game-theory.com/essays/nim-is-easy/)
7. [Misère play, Combinatorial Game Theory](https://www.combinatorial-game-theory.com/essays/misere-play/)
8. [Nim (1901): The First Stone-Taking Game to Be Solved, Puzzlebyrinth](https://puzzlebyrinth.com/en/articles/nim-1901)
9. [An Early Danish Computer Game: The case of Nim, Piet Hein, and Regnecentralen, IT University of Copenhagen](https://researcher.itu.dk/p/en/research-outputs/an-early-danish-computer-game-the-case-of-nim-piet-hein-and-regne)
10. [Experimental Study of the Game Exact Nim(5, 2), arXiv](https://arxiv.org/abs/2311.18772)
11. [Building Nim, arXiv](https://arxiv.org/abs/1502.04068)
12. [Faster Evaluation of Subtraction Games, arXiv](https://arxiv.org/abs/1804.06515)
13. [Play Nim and misère Nim, Absurd Tools](https://absurdtools.com/nim-game/)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 30, 2026; Oct 21, 2026 · Edited: Oct 15, 2026; Oct 21, 2026 · Last review: Oct 21, 2026*

Topic: 体育、游戏与休闲 › 棋牌与益智游戏 › 桌面游戏 › Modern designer board games › Modern abstract and tile-laying games

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/nim

English original: https://www.edgepedia.org/nim
