Shortest Distance from All Buildings - Problem

Imagine you're a city planner tasked with finding the perfect location for a new house that minimizes travel time to all existing buildings in the city!

You're given an m × n grid representing a city map where:

  • 0 represents empty land where you can build and walk freely
  • 1 represents an existing building (impassable)
  • 2 represents an obstacle like a lake or mountain (impassable)

Your goal is to find an empty land spot that has the shortest total walking distance to all buildings. You can only move up, down, left, or right (no diagonal movement).

Return the minimum total travel distance, or -1 if it's impossible to reach all buildings from any empty land.

Input & Output

example_1.py — Basic Case
$ Input: grid = [[1,0,2,0,1],[0,0,0,0,0],[0,0,1,0,0]]
Output: 7
💡 Note: The optimal house location is at (1,2). From this position: distance to building at (0,0) = 3, distance to building at (0,4) = 3, distance to building at (2,2) = 1. Total distance = 3 + 3 + 1 = 7.
example_2.py — Impossible Case
$ Input: grid = [[1,0,1],[0,2,0],[1,0,1]]
Output: -1
💡 Note: The building at (1,1) is blocked by obstacle (2), so no empty land can reach all buildings. Return -1.
example_3.py — Single Building
$ Input: grid = [[1,0,0],[0,0,0],[0,0,0]]
Output: 1
💡 Note: Only one building exists at (0,0). The optimal empty cell locations are the adjacent cells (0,1) and (1,0), each with distance 1 from the building. The minimum total distance is 1.

Constraints

  • m == grid.length
  • n == grid[i].length
  • 1 ≤ m, n ≤ 50
  • grid[i][j] is either 0, 1, or 2
  • There will be at least one building in the grid

Visualization

Tap to expand
Shortest Distance from All Buildings INPUT 1 0 2 0 1 0 0 0 0 0 0 0 1 0 0 1 = Building 0 = Empty Land 2 = Obstacle grid = [[1,0,2,0,1], [0,0,0,0,0], [0,0,1,0,0]] ALGORITHM (BFS) 1 Find Buildings Count all cells with value 1 (3 buildings found) 2 BFS from Each Building Calculate distance to all reachable empty cells 3 Sum Distances For each empty cell, sum distances to all buildings 4 Find Minimum Select cell with smallest total distance sum Cell (1,2) distances: To (0,0): 3 steps To (0,4): 3 steps To (2,2): 1 step Total: 7 FINAL RESULT B 9 X 9 B 10 8 7 8 10 11 9 B 9 11 Numbers = Total distance to all buildings B = Building, X = Obstacle Optimal: (1,2) OUTPUT 7 Min total distance Key Insight: Run BFS from each building (not from empty cells) to compute distances efficiently. For each empty cell, track: (1) sum of distances to all buildings, and (2) count of buildings reached. Only consider cells that can reach ALL buildings. Time: O(m*n*buildings), Space: O(m*n). TutorialsPoint - Shortest Distance from All Buildings | BFS Approach
Asked in
Google 42 Amazon 38 Meta 31 Microsoft 24
58.3K Views
Medium-High Frequency
~25 min Avg. Time
1.5K 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