Finding the Best Vertical Path in a Grid (with Jumps and Weights)

EmailTwitterLinkedInFacebookWhatsAppShare

In many grid-based problems, you need to choose exactly one column in each row to form a path.
But not all columns are equal — some cells are better (think “free road”), others are worse (think “toll road”), and some are blocked.

In our example:

  • X = good cell (free road)
  • Y = allowed but less preferred cell (toll road)
  • . = blocked cell (cannot use)

Our goal:

  1. Pick a column in every row so that we have a continuous path from top row to bottom row.
  2. Minimize the number of column changes (jumps) as we go from one row to the next.
  3. If two paths have the same number of jumps, choose the one with fewer Y cells.
  4. If there’s still a tie, choose the leftmost path (smallest column index).

Step 1: Understanding “jumps”

A jump happens when you switch from one column to a different column in the next row.
For example, this path has 1 jump:

Row 0: choose column 2
Row 1: choose column 2 (no jump)
Row 2: choose column 5 (JUMP!)

Step 2: Our DP Table: min_jumps and previous_choice

We use two tables:

  • min_jumps[i][j] → a pair (jump_count, y_count) meaning:
    minimum jumps and minimum Y count needed to reach row i at column j.
  • previous_choice[i][j] → the column index from the previous row that gave us this optimal result.

This lets us do dynamic programming row by row:

  • Start with the first row (min_jumps[0][j] = (0, y_count) if that cell is usable).
  • For each next row, try all possible previous columns and choose the best one based on our priority:

Step 3: The Code


Example Run

Output (example):

Meaning:

  • We stayed in column 0 for the all rows (no jump).
  • 0 Y count

Visual Explanation

Here’s a shorter, clear version of the explanation with visualization:

Example Grid

Row 0:  X  X  Y  .  X
Row 1:  X  .  Y  X  .
Row 2:  X  Y  X  .  X
  • X = free cell (no penalty)
  • Y = allowed but adds +1 penalty
  • . = blocked

Algorithm in Brief

  1. Initialize Row 0: Set jump count = 0 where cell ≠ ., Y adds y_count=1.
  2. For Each Row:
  3. Final Row: Choose column with smallest (jumps, Y count, column index) and backtrack to build the path.

Result for This Grid

Best Path: [0, 0, 0] (Column 0 for all rows)

  • Jumps: 0 (no column change)
  • Y Count: 0 (avoids Y completely)

Visual Path

The algorithm chose column 0 in every row because it has:

  • No jumps (stays in same column)
  • No Y cells (minimum penalty)
  • Lowest column index in case of ties

Why This Approach Works

This approach is efficient because:

  • We consider all possible columns for each row, so we never miss a better path.
  • The decision-making is lexicographic: prioritize jumps, then Y count, then leftmost.
  • It runs in O(R × C²) time (R = rows, C = columns), which is usually fast enough for small/medium grids. For larger grid, we can optimize with heuristics or pruning.

When to Use This

This technique is useful in any problem where:

  • You need to select exactly one option per row/level.
  • Each choice has a cost, and you want to minimize a combination of costs.
  • There is an adjacency relationship between rows (penalty for switching columns).

Examples outside this puzzle could include:

  • Scheduling problems where switching machines incurs cost.
  • Choosing routes across layered networks.
  • Grid-based puzzle solvers (like pathfinding with penalties).

Key Takeaways

  • Dynamic programming gives us a clean way to evaluate every possible path.
  • By comparing (jumps, y_count, column) at every step, we guarantee we choose the optimal path.
  • Backtracking with previous_choice recovers the actual path easily.

This approach can be used anywhere you need to pick one “best” option per row with priorities and penalties.

I hope this tutorial will create a good foundation for you. If you want tutorials on another topic or you have any queries, please send an mail at contact@spatial-dev.guru.

Leave a ReplyCancel reply

Discover more from Spatial Dev Guru

Subscribe now to keep reading and get access to the full archive.

Continue reading

Discover more from Spatial Dev Guru

Subscribe now to keep reading and get access to the full archive.

Continue reading