Skip to main content

Shortest Cell Path

MediumPremium

In a given grid of 0s and 1s, we have some starting row and column sr, sc and a target row and column tr, tc. Return the length of the shortest path from sr, sc to tr, tc that walks along 1 values only.

Each location in the path, including the start and the end, must be a 1. Each subsequent location in the path must be 4-directionally adjacent to the previous location.

It is guaranteed that grid[sr][sc] = grid[tr][tc] = 1, and the starting and target positions are different.

If the task is impossible, return -1.

Examples:

input: grid = [[1, 1, 1, 1], [0, 0, 0, 1], [1, 1, 1, 1]] sr = 0, sc = 0, tr = 2, tc = 0 output: 8 (The lines below represent this grid:) 1111 0001 1111 grid = [[1, 1, 1, 1], [0, 0, 0, 1], [1, 0, 1, 1]] sr = 0, sc = 0, tr = 2, tc = 0 output: -1 (The lines below represent this grid:) 1111 0001 1011

What algorithms may be useful for finding the shortest distance?

Since we're searching for shortest distance, try using breadth-first search first.

Typically in BFS, we have a graph for which each node has some number of neighbors. How could we build a graph from the inputs that would represent the distances described in the question?