Problem 534232 · medium · Level 05 Advanced Algorithms & Graphs

Fewest Moves Across the Warehouse

grids · BFS · shortest path

A warehouse floor is a grid floor where 0 is open floor and 1 is a shelf. A robot starts on the top-left cell (0, 0) and must reach the bottom-right cell. Each move goes to a side-adjacent open cell. Return the minimum number of moves needed, or -1 if the bottom-right cell cannot be reached (including when the start or the target is a shelf).

Examples

Input:  floor = [[0,0,0],
                 [1,1,0],
                 [0,0,0]]
Output: 4
Explanation: (0,0) -> (0,1) -> (0,2) -> (1,2) -> (2,2).

Input:  floor = [[0,1],
                 [1,0]]
Output: -1

Input:  floor = [[0]]
Output: 0

Constraints

  • 1 <= rows, cols <= 60
  • Target O(rows * cols) time.

Goals

  • Find a shortest path in an unweighted grid with obstacles
  • Return -1 cleanly when the target is unreachable or blocked
Starting Python…