PrepStack · Markdown HTML

Grid dynamic programming

Dynamic programming · HTML

Formal shape

dp[r][c] denotes an answer reaching or leaving cell (r,c). With right/down movement, dp[r][c]=cell(r,c) ⊕ Aggregate(dp[r-1][c],dp[r][c-1]). Iteration must be a topological order.

Recognition signals

False friends

SignalWhy it is different
Unrestricted four-way movementA row scan is no longer topological; use BFS/Dijkstra for shortest paths or another cyclic-state method.
Connectivity onlyWhen no path value is aggregated, DFS or BFS expresses the task directly.
A path bottleneck objectiveWith four-way movement and a maximum-along-path cost, minimax Dijkstra or answer search is a better model.

Core invariant

When cell (r,c) is processed, every predecessor state already meets its definition; the new state aggregates every legal way to enter the cell exactly as required.

State, transition, and processing order

ItemDefinition
StateAn optimum or count for coordinate (r,c), with an unreachable sentinel for blocked cells.
TransitionAggregate all legal predecessors by min, max, or sum and combine with the current cell; initialize the start independently.
Frontier / orderA row/column topological order matching movement. If only the previous row and current left state matter, compress to one row.

Python skeleton

def min_grid_cost(grid):
    if not grid or not grid[0]: return 0
    rows, cols = len(grid), len(grid[0])
    dp = [float('inf')] * cols
    dp[0] = 0
    for r in range(rows):
        for c in range(cols):
            from_up = dp[c]
            from_left = dp[c - 1] if c else float('inf')
            if r == 0 and c == 0:
                dp[c] = grid[r][c]
            else:
                dp[c] = grid[r][c] + min(from_up, from_left)
    return dp[-1]

Correctness sketch

  1. The start is correctly initialized from its unique empty predecessor.
  2. Topological iteration makes the upper and left subanswers correct before use.
  3. Every path into the current cell has one legal final predecessor, so aggregating all such correct candidates is complete; induction reaches the destination.

Complexity

TimeSpaceParameters
O(RC)O(C) compressed or O(RC) for the full tableR and C are dimensions; a wider dependency radius needs more retained rows.

Edge cases

Error patterns

PatternWhy it fails / fix
Scanning against dependency directionA transition reads an unfinished or overwritten state; draw arrows before loops.
Scattered first-row special casesSentinels or a padded border can unify transitions and reduce branching.
Treating a cyclic grid as a DAGFour-way movement creates cycles, so one scan cannot finalize states.
Overwriting the upper valueIn one-row compression, dp[c] is the old row while dp[c-1] is the new row.

Selection and exclusion rules

RelationTemplateBoundary
adjacent, not interchangeableGrid flood fillMoves form a DAG and the goal is counting or optimization.
dimensional-extensionLinear dynamic programmingThe two-dimensional topological state extends linear DP.

Original micro-example

PartDetail
ResultThe minimum route passes through the lower-left cell and costs 6.
SetupCosts are [[2,5],[1,3]], with only right or down moves.
TraceLeft-bottom costs 3, right-top 7; bottom-right is min(3,7)+3=6.

Recall prompts

My notes