目录
正在加载目录…
正在阅读专栏 Algorithms:CLRS + MIT 6.006 第 22 篇 · 共 24 篇 · 92%

Dynamic Programming IV:Guitar Fingering、Tetris 与 Super Mario Bros.

发布于 2026-09-18 19:53 · 最后编辑于 2026-09-18 19:53 · 版本 v1 · 字数 5,863 · 预计阅读 12 分钟 👁 16 次阅读

Algorithms:Dynamic Programming IV:Guitar Fingering, Tetris and Super Mario Bros.

本文按 MIT 6.006 Fall 2011 Lecture 22《Two Kinds of Guessing:Piano/Guitar Fingering, Tetris Training, Super Mario Bros.》的顺序整理。重点是 DP 中的第二种 guessing:为了让 parent subproblem 获得足够的 solution information,主动增加 subproblems、扩充 state,而不是强行使用过小的 state。

TL;DR

  • 第一种 guessing:在已经定义好的 state 中,猜 solution 的下一部分,枚举要连接哪些 subproblems。
  • 第二种 guessing:在设计 state 时增加更多 subproblems,用 state 记住 solution 的额外结构;这正是 knapsack、fingering、Tetris 等问题需要的技巧。
  • Guitar fingering:只记录当前 note 不够,还要记录当前 note 使用的 finger;state 数为 nF,复杂度 O(nF²)。
  • Tetris:只记录 piece index 不够,还要记录当前 board profile;state 约为 O(nh^w),复杂度 O(nwh^w)。
  • Super Mario Bros.:需要把 screen configuration、Mario 状态、objects、velocity、score 和 time 等信息纳入 state,得到一个可能非常大的 pseudopolynomial / exponential state space。
  • 原则:若 child solution 的某个 feature 会影响 parent 的 choice,就必须把该 feature 放进 child 的 state。

目录

章节说明
Lecture 22:Dynamic Programming IV第二种 guessing
Two kinds of guessing扩充 state 与枚举 choice
Piano and guitar fingeringcurrent finger state
Tetris trainingboard profile state
Super Mario Bros.configuration state
如何判断需要扩充 statestate-design rule
小结Unit 7 的收束

Lecture 22:Dynamic Programming IV

本讲是 dynamic programming 单元的最后一讲。前几讲通常在一个已经定义的 state 上猜 choice:

  • Text Justification:猜第一行在哪里结束;
  • Edit Distance:猜 delete、insert 或 replace;
  • Knapsack:猜 take 还是 skip。

本讲加入一个更困难、也更通用的设计问题:

如果当前 state 太小,无法告诉 parent subproblem 哪些信息会影响后续 decision,该怎么办?

答案是增加 state,让更多 solution features 被显式记录。代价是 subproblems 数量增加,但换来的结果是 recurrence 能够正确表达未来 decisions。

Two kinds of guessing

Kind A:guess which subproblems to use

这是常见的 guessing:

  1. state 已经定义;
  2. 枚举一个 local choice;
  3. 根据 choice 连接到更小的 subproblems;
  4. 对这些 subproblem values 做 min、max 或 sum。

例如 edit distance 在当前 state 中猜三种 edits。

Kind B:create more subproblems

第二种 guessing 发生在第 1 步:重新设计 state,主动创建更多 subproblems,用它们记住 solution 的更多 features。

这在以下情形非常重要:

  • child solution 的最终 value 相同,但某个隐藏 feature 不同;
  • parent 需要根据这个 feature 才能判断下一步;
  • 如果只保留一个 value,就丢失了 future decision 所需的信息。

Knapsack 已经展示了这个思想:只按 item suffix 定义 state 不够,还要加入 capacity。不同 capacity 对同一 suffix 会产生不同的可行 solution,而 parent 必须知道剩余容量。

Piano and guitar fingering

问题

给定一段由 n 个 single notes 组成的 music piece,使用右手弹奏。每个 note 可以用 fingers 1 到 F 中的一个弹奏。目标是最小化整体 fingering difficulty。

difficulty 可以由一个函数表示:

Code
d(p, f, q, g)

它表示从 note p 使用 finger f,移动到 note q 使用 finger g 的难度。

可以把音乐规则编码进这个函数:

  • 某些 finger transitions 不舒服;
  • 某些 stretch 方向更困难;
  • legato 可能不允许连续使用同一个 finger;
  • weak-finger rule 可能惩罚 fingers 4 和 5;
  • 特定的 finger pair transitions 可能有不同 cost。

First attempt:state 太小

一种看似自然的 state 是:

Code
DP[i] = 从 note i 开始的最小 difficulty

然后猜 note i 使用的 finger f。但 recurrence 需要计算从当前 note 到下一个 note 的 transition difficulty,而这个 cost 同时取决于下一个 note 使用的 finger。

如果 child state 只告诉我们从 i+1 开始的最小 difficulty,却没有告诉我们 i+1 的 first finger,那么 parent 无法正确计算 transition。state 丢失了必要 information。

Correct state

定义:

Code
DP[i, f] = 从 note i 开始,且 note i 使用 finger f 的最小 difficulty

subproblems 数量为 nF。对当前 finger f,猜下一个 note 使用的 finger g:

Code
DP[i, f]
= min over g
    { d(note[i], f, note[i+1], g) + DP[i+1, g] }

最后一个 note 的 base case 为 0:

Code
DP[n, f] = 0

原问题的第一根手指也需要猜:

Code
answer = min over f DP[0, f]

Complexity

每个 state 有 F 个 choices:

Code
number of subproblems = nF
time per subproblem = Θ(F)
total time = O(nF²)
space = O(nF)

完成 DP 后,保存每个 DP[i,f] 的 best next finger,就可以沿 parent pointers 恢复整段 fingering。

Guitar 的扩展

在 guitar 上,同一个音高可能由不同 strings 和不同 fingers 产生。因此可以把“finger”重新定义为:

Code
finger + string

如果有 F 根手指和 S 根 strings,state 的 choices 会从 F 扩大到 F·S。若一次演奏多个 notes,例如 chord,则 state 需要记录多个 fingers 到 notes 或 strings 的 assignment,subproblem 数会进一步增长。

这不是 recurrence 写错,而是 solution 的结构本身更丰富,需要更大的 state 才能表达它。

Tetris training

简化问题

给定 n 个 Tetris pieces、一个空 board,以及较小的 board width w:

  • 每个 piece 可以选择 orientation;
  • 选择 piece 的 x coordinate;
  • piece 下落直到碰到其他 piece;
  • full rows 不 clear;
  • 目标是在 height h 内 survive 完整 sequence。

这些 artificial assumptions 让问题可以用 DP 讨论;更一般的 Tetris 变体可能具有更高复杂度,甚至 NP-complete。

First attempt:只记录 piece index

初步 state:

Code
DP[i] = 从第 i 个 piece 开始能否 survive

当前 piece 的 drop choice 数量大约是 O(w)。但无论 piece i 如何放置,DP[i+1] 都只提供一个 yes/no value;它没有告诉我们 prefix 已经把 board 堆成了什么形状。

同样的剩余 pieces,面对不同 board profile,答案可能完全不同。因此 DP[i] 丢失了 parent 所需的 information。

Correct state:board profile

用每一列的当前高度表示 board profile:

Code
h = (h₀, h₁, ..., h₍w−1₎)

重新定义:

Code
DP[i, h] = 给定当前 board profile h,从 piece i 开始能否 survive

状态数量约为:

Code
O(nh^w)

这里的 h^w 表示每一列高度有大约 h 种可能。对每个 state,枚举当前 piece 的合法 moves m:

Code
DP[i, h]
= max over valid moves m
    DP[i+1, next_profile(h, m)]

对于 survival decision,max 可以理解为只要存在一种 move 能成功即可;也可以把它写成 boolean OR。

evaluation order 是:

Code
for i in reversed(range(n)):
    for every board profile h:
        evaluate all valid moves

每个 state 有 O(w) 左右的 choices,总 time 为:

Code
O(nwh^w)

由于 state 已包含足够的 prefix information,parent pointer 可以记录每个 profile 下的最佳 move,并恢复完整的 placement sequence。

Super Mario Bros.

问题抽象

假设给定一个固定 level,目标是最大化 score、最小化 time,或判断是否能成功通关。若 screen width 和 height 较小,可以把游戏视为有限 state graph。

一个 configuration 可能需要包括:

  • 当前 screen 在 level 中的位置;
  • screen 上每个 object 的位置和状态;
  • bricks 是否被破坏;
  • coins、mushrooms 等 collectibles 是否已经获得;
  • monsters 的位置;
  • Mario 的位置、状态和 velocity;
  • 剩余 time;
  • 当前 score;
  • 其他会影响未来 transitions 的游戏信息。

为什么简单 state 不够

只记录“已经走到 level 的第 i 个位置”远远不够:

  • 同一位置可能有不同的 Mario velocity;
  • 同一 screen 可能有不同 monsters;
  • question-mark box 是否已经击中会影响未来;
  • score 与 time 可能影响目标;
  • 已经改变的 board objects 不能被忽略。

因此要定义 configuration state:

Code
DP[configuration] = 从该 configuration 开始的最优未来结果

每个 valid action 是一个 outgoing transition,recurrence 可以是:

Code
DP[c]
= max over actions a
    { immediate_score(c,a) + DP[next(c,a)] }

或在最小化 time 时使用 min。

State space 的大小

如果 screen 的每个 unit square 有常数种状态,screen 的 configuration 数量可能是:

Code
constant^(w·h)

再乘上 score 的可能数量 S、time 的可能数量 T,以及 Mario velocity 等状态。因此整体 subproblem 数可能非常大。

由于 score 和 time 是 numerical values,显式按数值展开时还可能出现 pseudopolynomial behavior;而 screen configuration 本身则可能带来 exponential dependence on screen dimensions。

这说明 DP 的“polynomial”并不意味着任何现实游戏都能高效求解。它只说明:在固定且足够小的 state dimensions 下,可以把动态游戏过程看成一个 finite DAG 或 state graph。

如何判断需要扩充 state

可以用下面的问题检查当前 state:

  1. 两个相同的 state value,是否可能对应不同的 solution features?
  2. parent 的下一步 choice 是否会受到这些 features 影响?
  3. child state 是否需要向 parent 汇报某个 boundary condition?
  4. 如果删除 prefix,哪些信息仍然会影响 suffix 的最优解?
  5. 能否把这些信息加入 state,同时保持 state space 可分析?

典型 boundary information 包括:

  • 当前使用的 finger;
  • 当前 board profile;
  • remaining capacity;
  • 最后一项的颜色、字符或位置;
  • 当前机器、游戏或环境 configuration。

原则可以概括为:

State 必须是 future-relevant information 的充分摘要。

既不能忘记会影响未来 choices 的信息,也不应保存与未来无关的完整历史。

两种 guessing 的统一视角

方式发生位置例子
猜连接哪些 subproblemsrecurrence 中edit distance 的 delete / insert / replace
增加 state 以记住更多结构subproblem definition 中finger、board profile、capacity、game configuration

第二种 guessing 往往会增加 subproblem 数量,但它使 parent 能够正确评估 child solution。很多 DP 的难点不在 min/max,而在决定“为了让 recurrence 正确,我必须记住什么”。

小结

  • DP 的第二种 guessing 是主动扩充 state,以记住对未来 decisions 有影响的 solution features。
  • Piano/guitar fingering 必须把当前 finger 放进 state,否则无法计算相邻 notes 的 transition cost。
  • Tetris 必须记住 board profile;只记录 piece index 会丢失 prefix 的形状信息。
  • Super Mario 的 state 需要包含 screen、objects、Mario dynamics、score 和 time 等 configuration information。
  • 扩充 state 会增加 subproblem 数量,可能产生 pseudopolynomial 或 exponential state space。
  • 一个正确的 DP state 应该是 future-relevant information 的充分摘要;它是设计 recurrence 的基础。

参考资料

导航

← 返回列表
← 上一篇 Dynamic Programming III:Parenthesization、Edit Distance 与 Knapsack 下一篇 → Computational Complexity:P、NP、Reductions 与 Completeness

评论 (0)

暂无评论,来留下第一条吧。
登录 或 注册 后才能发表评论