Warehouse Route Optimizer - Problem

You're managing a large warehouse with a complex grid layout where delivery robots need to find the shortest path from a starting position to a destination. The warehouse has several complications:

Grid Constraints:

  • 0 represents open corridors where robots can move freely
  • -1 represents blocked sections (walls, shelves, obstacles)
  • Positive integers represent weighted corridors with movement costs

Movement Rules:

  • Robots can move in 4 directions (up, down, left, right)
  • Some paths may be one-way based on conveyor belts or traffic flow
  • Movement cost from cell A to cell B is determined by the destination cell's weight

Given a warehouse grid, starting position, and destination position, find the minimum total cost to reach the destination. If no path exists, return -1.

Input & Output

Example 1 — Basic Warehouse Grid
$ Input: grid = [[0,1,2],[1,0,3],[2,3,0]], start = [0,0], end = [2,2]
Output: 6
💡 Note: Optimal path: (0,0) → (1,0) → (2,1) → (2,2). Cost = 0 + 1 + 3 + 0 = 4. Wait, let me recalculate: movement cost is destination cell's weight, so (0,0)→(1,0) costs 1, (1,0)→(2,0) costs 2, (2,0)→(2,1) costs 3, (2,1)→(2,2) costs 0. Total = 1+2+3 = 6.
Example 2 — Blocked Path
$ Input: grid = [[0,1,-1],[1,-1,2],[3,2,0]], start = [0,0], end = [2,2]
Output: 9
💡 Note: Direct path blocked by -1 cells. Must go: (0,0) → (0,1) → (1,0) → (2,0) → (2,1) → (2,2). Cost = 1 + 1 + 3 + 2 + 0 = 7. Actually, checking valid moves more carefully: (0,0)→(1,0) costs 1, then (1,0)→(2,0) costs 3, then (2,0)→(2,1) costs 2, then (2,1)→(2,2) costs 0. Total = 6. Or (0,0)→(0,1) costs 1, but (0,1)→(1,1) blocked. Let me trace: viable path is (0,0)→(1,0)→(2,0)→(2,1)→(2,2) = 1+3+2+0 = 6.
Example 3 — No Path Available
$ Input: grid = [[0,-1],[−1,0]], start = [0,0], end = [1,1]
Output: -1
💡 Note: All adjacent cells to start (0,0) are blocked (-1), so no path exists to reach destination (1,1).

Constraints

  • 1 ≤ grid.length, grid[0].length ≤ 100
  • -1 ≤ grid[i][j] ≤ 1000
  • 0 ≤ start[0], start[1] < grid.length
  • 0 ≤ end[0], end[1] < grid.length

Visualization

Tap to expand
INPUTALGORITHMRESULTS121X323EWarehouse Grid:Start: (0,0) End: (2,2)Costs: 0=free, numbers=tollX = blocked areas1Initialize distances2Use priority queue3Process min cost cell4Update neighbor costsDijkstra's AlgorithmGuarantees optimal pathOptimal Path FoundTotal Cost: 6Route: (0,0)→(1,0)→(2,0)→(2,1)→(2,2)Step costs: 1 + 2 + 3 + 0 = 6✓ Guaranteed minimum costKey Insight:Dijkstra's priority queue always expands the cheapest partial path first,guaranteeing that when we reach the destination, we've found the optimal route.TutorialsPoint - Warehouse Route Optimizer | Dijkstra's Algorithm
Asked in
Amazon 45 Google 38 Microsoft 32 Uber 28
28.5K Views
High Frequency
~35 min Avg. Time
842 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen