KatInterview

Minimum-Cost Path with Fuel Stops on a Grid

Dynamic ProgrammingAuto-graded · 17 test cases
HardAuto-gradedDynamic Programmingdynamic programminggridstate

Last updated

Tests grid DP with an extra state dimension: navigate an N×M grid from top-left to bottom-right tracking remaining fuel, refueling at marked cells, minimizing total toll cost.

Minimum-Cost Path with Fuel Stops

You are driving a vehicle across a rectangular grid of toll roads. The grid has N rows and M columns. Your vehicle starts at the top-left cell (0, 0) and must reach the bottom-right cell (N-1, M-1), moving only right or down at each step.

Each cell (r, c) has a toll cost given by grid[r][c] (a non-negative integer). You pay the toll when you enter a cell — you do not pay to stand at your starting cell (0, 0).

Your vehicle has a fuel tank with capacity F. You begin with a full tank (F units of fuel). Every move (right or down) consumes exactly 1 unit of fuel. If your fuel reaches 0 and you have not yet arrived at (N-1, M-1), you cannot move further — that path is invalid.

Certain cells are designated fuel stations, given as a list of [r, c] pairs. When you enter a fuel station cell, your tank is immediately refueled to F (full capacity) before any further moves. You pay the toll of the station cell normally.

Your goal is to find the minimum total toll cost of any valid path from (0, 0) to (N-1, M-1).

  • If no valid path exists (every path runs out of fuel before reaching the destination), return -1.
  • The destination cell (N-1, M-1) may itself be a fuel station (it doesn't matter, since you stop there).
  • The starting cell (0, 0) may be a fuel station; if so, entering it would refuel — but since you start there without "entering," it has no refueling effect (your tank is already full). Its toll is also not paid (you don't enter it).

Constraints

  • 1 ≤ N, M ≤ 40
  • 1 ≤ F ≤ N + M (fuel capacity; note a full tank is always enough to traverse the shortest possible path with no obstacles if F = N + M - 2)
  • 0 ≤ grid[r][c] ≤ 1000
  • 0 ≤ len(stations) ≤ N * M
  • Each element of stations is a valid [r, c] within the grid; duplicates in stations are ignored.
  • There is always at least one cell that is reachable in a single move from (0, 0) (i.e., N ≥ 1 and M ≥ 1, so the grid is non-trivial when N + M > 1).
  • When N = 1 and M = 1, the answer is 0 (already at destination, no moves needed).

Examples

Example 1

Input: min_cost_fuel_path([[0, 2, 3], [1, 5, 2], [4, 1, 0]], [[1, 0], [1, 2]], 3)

Output: 6

Example 2

Input: min_cost_fuel_path([[0, 1], [1, 0]], [], 1)

Output: -1

Example 3

Input: min_cost_fuel_path([[0]], [], 1)

Output: 0

Hints

Hint 1

Define your DP state as (row, col, fuel_remaining) and use memoization or a priority-queue (Dijkstra) over this 3-D state space. The number of states is at most N × M × F, which is manageable.

Hint 2

When you enter a fuel-station cell, set the fuel dimension to F before recording the state — this collapses many states and prevents infinite loops.

Hint 3

Use Dijkstra's algorithm treating each (row, col, fuel) triple as a node and edge weights as the toll of the destination cell. This guarantees the minimum-cost path even when toll costs vary.