公平游戏的Sprague-Grundy定理

原文地址:Sprague-Grundy Theory 作者:unmoral

The Sprague-Grundy theory of impartial games

公平游戏的Sprague-Grundy定理

November 9, 2005

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

公平游戏是一种双人游戏,在游戏中双方都有完整的信息,没有牵涉,任何状态的合法操作对双方来说都是相同的。

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

一个公平游戏可以抽象地用一个有向无环图来表示,这个图中每个点都对应这一个状态,每条有向边代表从一个状态到另一个状态的合法操作。

[转载]Sprague-Grundy <wbr>Theory

We can imagine that a token is initially placed on some vertex,and the two players take turns moving the token from its currentvertex to one of its direct followers. The game ends when the tokenreaches 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

P- 和 N-状态

We can classify each position in the game according to whetherit is a first- or a second-player win, if both players playoptimally starting from that position. A first-player-win positionis known as an N-position (because thenext player is to win), while asecond-player-win position is known as a P-position(because the previous player is towin).

如果双方都按照最佳策略进行游戏,我们可以将游戏中的每个状态依据其是先手必胜还是后手必胜分类。
一个先手胜状态被认为是一个N-状态(因为下一个玩家即将获胜),一个后手胜状态被认为是一个P-状态(因为前一个玩家即将获胜)

The P- and N-positions can be characterizedinductively as follows:

P-和N-状态归纳性地描述如下:

  • A vertex v is a P-positionif and only if all itsdirect followers are N-positions.
  • A vertex v is an N-positionif and only if it has some P-positionfollower.
  • 一个点v是P-状态当且仅当它的所有后继都为N-状态
  • 一个点v是N-状态当且仅当它的一些后继是P-状态

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

这个归纳从汇点开始,汇点是P-状态因为它显然满足P-状态的要求。

[转载]Sprague-Grundy <wbr>Theory

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

游戏的P-和N-状态的信息提供了它的必胜策略。如果轮到我们且游戏处在一个N-状态,我们应该转移到一个P-状态。接着我们的对手就会被迫进入N-状态,依此类推。我们最终会移入一个汇点并获得胜利。

Sums of games

游戏的和

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

如果G1G2是公平游戏,那么他们的和G1 +G2是另一个公平游戏,玩法如下:每个回合,一个玩家选择G1,G2 中的一个(随便哪个他希望的)然后玩它,不碰另一个游戏。当G1G2都不能操作时游戏结束。

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

形式上,如果 G1 = (V1,E1) 和 G2 =(V2, E2)是游戏图,那么他们的和Gsum = (Vsum,Esum) 规定为:

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

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

现在,假定我们给出两个游戏G1G2。如果我们只知道单个游戏的P-状态和N-状态我们能够正确地玩好游戏和G1+G2吗?答案是否定的。不难看出两个P-状态的和总是P-状态,P-状态和N-状态的和总是N-状态。但是两个N-状态的和既可能是P-状态也可能是N-状态。因此,只知道单个游戏的P-状态和N-状态是不够的。

To play sums of games correctly we need a generalization of thenotion of P- and N-positions, which is known asthe Sprague-Grundy function (or Grundy functionfor short).

为了正确地玩好游戏和我们需要推广P-状态和N-状态,它就是Sprague-Grudy函数(或者简称为Grundy函数)。

The Sprague-Grundy function

Sprague-Grundy 函数

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

N = {0, 1, 2, 3, ...} 为自然数的集合。Sprague-Grundy函数给游戏中的每个状态分配了一个自然数。结点v的Grundy值等于没有在v的后继的Grundy值中出现的最小自然数。

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

形式上:给定一个有限子集 SN,令mexS(最小排斥值)为没有出现在S中的最小自然数

mex S = min (N S).

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

现在,给定一个游戏图G=(V,E),其Sprague-Grundy函数g:VN归纳定义为

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

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

从G的汇点开始归纳,可知它的Grundy值为0

[转载]Sprague-Grundy <wbr>Theory

The Sprague-Grundy function satisfies two importantproperties:

Sprague-Grundy函数满足两个重要性质:

  • A vertex v is a P-positionif and only if g(v) = 0.
  • If G = G1 +G2 and v =v1v2 is a position inG, then g(v) is the bitwise XOR of the binaryrepresentations of g(v1) andg(v2): g(v) =g(v1) ⊕g(v2).
  • 点v是一个P-状态当且仅当g(v)=0
  • 如果G = G1 +G2v =v1v2是G的一个状态,那么g(v)为g(v1) 和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.

运算⊕也称作nim和。举个例子,3 ⊕ 5 = 0112 ⊕ 1012 =1102 = 6。类似地,3 ⊕ 6 = 5 且 5 ⊕ 6 = 3。

It is not hard to prove the above two properties byinduction.

不难利用归纳法证明上面两个性质。

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

根据这些性质有v = v1v2是P-状态当且仅当g(v1) =g(v2), 因为这是唯一能够使得nim和为0的途径。

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

无疑,游戏的求和是满足交换律和结合律的运算,nim和运算也是。
因此,我们可以通过获知单个游戏的Grundy函数来正确地玩好任意数目游戏和。

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 movein some component game that causes the nim sum to become 0. Weshould make that move, and then our opponent will be forced to makethe nim sum nonzero again. Eventually, we will be the ones to makethe last move in the last game, bringing the nim sum to 0 for thefinal time.

我们的策略如下:如果轮到我们且游戏的Grundy值给出了一个非0的nim和,那么必然在游戏的某个组分中存在一个操作使得nim和变为0。我们应该执行这个操作,那么接着我们的对搜就被迫再次使得nim和非0。最终,我们将成为在最后一个游戏执行最后一个操作的人,最后将nim和变为0.

The game of Nim

Nim游戏

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

最基本的公平游戏是Nim堆。一个Nim堆由确定数目代币组成。在每个回合,一个玩家从堆上拿走1到整堆中任意数目的代币。拿空整堆的人获得胜利。

[转载]Sprague-Grundy <wbr>Theory

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

这个游戏如果独立看是没有意义的:先手玩家可直接拿走所有代币并立即获得胜利!
但是如果我们将各种大小的Nim堆加在一起,我们就得到了著名的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 ofits pile sizes.

大小为n的Nim堆的Grundy值为n。因此,Nim游戏中每个状态的Grundy值为每堆大小的Nim和。

Games that decompose into sums of themselves

一些分解成自身和的游戏

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

Sprague-Grundy定理最自然的应用就是一些分解成自身和的一些游戏。

Consider the following game: There is a checkerboard of sizem × n, and there is an unlimited supply ofpolyominoes of a certain shape. On each turn, a player places apolyomino on an empty place on the board, and the player who cannotmake a move is the loser:

考虑下面这个游戏:有一个大小为m*n的棋盘,且有无限数目某特定形状的骨牌供应。在每个回合,玩家在棋盘上一个空位放置一个骨牌,不能放骨牌的玩家就是败者。

[转载]Sprague-Grundy <wbr>Theory

During the course of the game, the board will gradually splitinto separate regions, for which we can calculate their Grundyvalues independently:

在游戏期间,棋盘会逐渐分成不同的区域,对其我们可以分别计算Grundy值。

[转载]Sprague-Grundy <wbr>Theory

For another example consider Grundy's game. A positionin this game consists of a number of piles of tokens of varioussizes, and a move consists of taking one pile and splitting it intotwo unequal piles. Thegame ends when there are only piles of sizes 1 and 2 left, whichcannot be split further.

再举个例子,考虑Grundy游戏。这个游戏的一个状态由一些不同大小的代币堆组成,一次操作由取下一堆和分成两个不等的堆组成。当所有堆的大小只有1和2的时候游戏结束,因为它不能再分。

 

Let g(n) be the Grundy value of a single pileof size n. The sequence g(n) starts asfollows:

令g(n)为单个大小为n的堆的Grundy值。数列g(n)如下:

 

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

 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值