The Sprague-Grundy theory of impartial games

本文介绍了一种名为Sprague-Grundy函数的数学工具,用于分析无偏博弈中的最优策略。文章详细阐述了如何通过该函数确定游戏状态的赢输性质,并以Nim游戏为例进行了说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



An impartial game is a two-player game in which both players have complete information, no chance is involved, and the legal moves from each position are the same for both players. We will deal with the normal play rule, in which the last player to move is the winner.

An impartial game can be abstractly represented by a directed acyclic graph, in which each vertex corresponds to a position, and each directed edge represents a legal move from one position to another.

We can imagine that a token is initially placed on some vertex, and the two players take turns moving the token from its current vertex to one of its direct followers. The game ends when the token reaches a sink, which is a vertex with no outgoing edges, and then the last player to have moved is the winner.

P- and N-positions

We can classify each position in the game according to whether it is a first- or a second-player win, if both players play optimally starting from that position. A first-player-win position is known as an N-position (because the next player is to win), while a second-player-win position is known as a P-position (because the previous player is to win).

The P- and N-positions can be characterized inductively as follows:

  • A vertex v is a P-position if and only if all its direct followers are N-positions.
  • A vertex v is an N-position if and only if it has some P-position follower.

The induction starts at the sinks, which are P-positions because they vacuously satisfy the P-position requirement.

Knowledge of the P- and N-positions of a game provides the winning strategy for it: If it is our turn and the game is in an N-position, we should move into a P-position. Then our opponent will be forced to move into an N-position, and so on. Eventually we will move into a sink and win.

Sums of games

If G1 and G2 are impartial games, then their sum G1 + G2 is another impartial game, which is played as follows: On each turn, a player chooses one of G1, G2 (whichever he wants) and plays on it, leaving the other game untouched. The game ends when no moves are possible on G1 nor on G2.

Formally, if G1 = (V1, E1) and G2 = (V2, E2) are game-graphs, then their sum Gsum = (Vsum, Esum) is given by:

Vsum = V1 × V2,
Esum = {(v1v2, w1v2) | (v1, w1) ∈ E1} ∪ {(v1v2, v1w2) | (v2, w2) ∈ E2}.

Now, suppose we are given two games G1 and G2. Can we correctly play their sum G1 + G2 if we just know the P- and N-positions of the individual games? It turns out that the answer is no. It is not hard to see that the sum of two P-positions is always a P-position, and the sum of a P-position and an N-position is always an N-position. But the sum of two N-positions can either be a P- or an N-position. Therefore, just knowing the P- and N-positions of the individual games is not enough.

To play sums of games correctly we need a generalization of the notion of P- and N-positions, which is known as the Sprague-Grundy function (or Grundy function for short).

The Sprague-Grundy function

Let N = {0, 1, 2, 3, ...} be the set of natural numbers. The Sprague-Grundy function assigns to each position in a game a natural number. The Grundy value of a vertex v equals the smallest natural number that does not appear among the Grundy values of v's direct followers.

Formally: Given a finite subset SN, let mex S (the minimum excluded value) be the smallest natural number not in S:

mex S = min (N / S).

Now, given a game-graph G = (V, E), its Sprague-Grundy function g: VN is defined inductively by

g(v) = mex {g(w) | (v, w) ∈ E}.

This induction starts at the sinks of G, which receive a Grundy value of 0.

The Sprague-Grundy function satisfies two important properties:

  • A vertex v is a P-position if and only if g(v) = 0.
  • If G = G1 + G2 and v = v1v2 is a position in G, then g(v) is the bitwise XOR of the binary representations of g(v1) and g(v2):

    g(v) = g(v1) ⊕ g(v2).

The operation ⊕ is also called nim sum. For example, 3 ⊕ 5 = 0112 ⊕ 1012 = 1102 = 6. Similarly, 3 ⊕ 6 = 5 and 5 ⊕ 6 = 3.

It is not hard to prove the above two properties by induction.

From these properties it follows that v = v1v2 is a P-position if and only if g(v1) = g(v2), since this is the only way the nim sum can come out 0.

Clearly, the sum of games is a commutative and associative operation, as is the nim sum operation. Therefore, we can correctly play the sum of any number of games by knowing the Grundy function of each individual game.

Our strategy is as follows: If it is our turn and the games' Grundy values give a nonzero nim-sum, then there must exist a move in some component game that causes the nim sum to become 0. We should make that move, and then our opponent will be forced to make the nim sum nonzero again. Eventually, we will be the ones to make the last move in the last game, bringing the nim sum to 0 for the final time.

The game of Nim

The most fundamental impartial game is the Nim pile. A Nim pile consists of a certain number of tokens. On each turn, a player removes from the pile any number of tokens between one token and the entire pile. The player who empties the pile wins.

This game by itself is of course trivial: The first player can just take all the tokens and win immediately! But if we add together Nim piles of various sizes, we get the famous game of Nim.

The Grundy value of a Nim pile of size n is n. Therefore, the Grundy value of a position in Nim is the nim sum of its pile sizes.

Games that decompose into sums of themselves

The most natural application of the Sprague-Grundy theory is for games that naturally decompose into sums of themselves.

Consider the following game: There is a checkerboard of size m × n, and there is an unlimited supply of polyominoes of a certain shape. On each turn, a player places a polyomino on an empty place on the board, and the player who cannot make a move is the loser:

During the course of the game, the board will gradually split into separate regions, for which we can calculate their Grundy values independently:

For another example consider Grundy's game. A position in this game consists of a number of piles of tokens of various sizes, and a move consists of taking one pile and splitting it into two unequal piles. The game ends when there are only piles of sizes 1 and 2 left, which cannot be split further.

Let g(n) be the Grundy value of a single pile of size n. The sequence g(n) starts as follows:

n:1234567891011121314151617181920...
g(n):00102102102132132430

Although these values have been calculated to around n = 235, this sequence is still not completely understood. It is possible that the sequence will eventually become periodic, although this has not been seen to happen yet. See the following page by Achim Flammenkamp for more information.

The theory of impartial games was discovered independently by Roland P. Sprague in 1936 and P. M. Grundy in 1939.


 

width="728" scrolling="no" height="90" frameborder="0" align="middle" marginwidth="0" marginheight="0" src="http://download1.youkuaiyun.com/down3/20070601/01184120111.htm">
资源下载链接为: https://pan.quark.cn/s/22ca96b7bd39 在 IT 领域,文档格式转换是常见需求,尤其在处理多种文件类型时。本文将聚焦于利用 Java 技术栈,尤其是 Apache POI 和 iTextPDF 库,实现 doc、xls(涵盖 Excel 2003 及 Excel 2007+)以及 txt、图片等格式文件向 PDF 的转换,并实现在线浏览功能。 先从 Apache POI 说起,它是一个强大的 Java 库,专注于处理 Microsoft Office 格式文件,比如 doc 和 xls。Apache POI 提供了 HSSF 和 XSSF 两个 API,其中 HSSF 用于读写老版本的 BIFF8 格式(Excel 97-2003),XSSF 则针对新的 XML 格式(Excel 2007+)。这两个 API 均具备读取和写入工作表、单元格、公式、样式等功能。读取 Excel 文件时,可通过创建 HSSFWorkbook 或 XSSFWorkbook 对象来打开相应格式的文件,进而遍历工作簿中的每个 Sheet,获取行和列数据。写入 Excel 文件时,创建新的 Workbook 对象,添加 Sheet、Row 和 Cell,即可构建新 Excel 文件。 再看 iTextPDF,它是一个用于生成和修改 PDF 文档的 Java 库,拥有丰富的 API。创建 PDF 文档时,借助 Document 对象,可定义页面尺寸、边距等属性来定制 PDF 外观。添加内容方面,可使用 Paragraph、List、Table 等元素将文本、列表和表格加入 PDF,图片可通过 Image 类加载插入。iTextPDF 支持多种字体和样式,可设置文本颜色、大小、样式等。此外,iTextPDF 的 TextRenderer 类能将 HTML、
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值