Return the cells of the shortest path from S to P in a grid with obstacles

Quick Overview

Given a grid of open cells and obstacles with one start and one destination, return the list of cells on a shortest four-direction path, breaking ties with a fixed neighbour order. It tests breadth-first search, parent tracking and path reconstruction on a grid.

Return the cells of the shortest path from S to P in a grid with obstacles

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given an `N x M` grid as an array of strings `grid`. Each character is one of the following: - `S` marks the start cell (exactly one). - `P` marks the destination cell (exactly one). - `.` marks an open cell. - `#` marks an obstacle. You may move one step up, down, left or right into any cell that is not an obstacle. Return the shortest path from `S` to `P` as a list of `[row, col]` cells, starting with `S`'s cell and ending with `P`'s cell. If `P` cannot be reached, return an empty list. ### Function Signature ```python def shortest_path(grid: list[str]) -> list[list[int]]: ``` ### Rules - Rows are numbered from the top and columns from the left, both starting at 0. Up means row - 1 and left means column - 1. - Several shortest paths can exist, so the answer must be unique: run BFS from `S`, exploring neighbours in the order up, down, left, right, and record a cell's parent only the first time the cell is reached. Return the path obtained by following parents back from `P`. - A cell is reached when it is first found as an in-grid, non-obstacle neighbour of the cell being expanded. That expanded cell becomes its parent and is never replaced. `S` has no parent. ### Constraints - `1 <= N, M <= 100` - `grid[i].length == M` - `grid` contains exactly one start cell `S` and exactly one destination cell `P` - Every `grid[i][j]` is one of the four characters `S`, `P`, `.` or `#` (start, destination, open cell or obstacle) ### Examples ```text Input: grid = ["S..",".#.","..P"] Output: [[0,0],[1,0],[2,0],[2,1],[2,2]] ``` There are two shortest paths of 4 moves: right-right-down-down and down-down-right-right. BFS checks down before right, so (1,0) is queued before (0,1). Following the parents of the cells reached first gives (0,0) -> (1,0) -> (2,0) -> (2,1) -> (2,2). ```text Input: grid = ["....",".S#.","..#P","...."] Output: [[1,1],[0,1],[0,2],[0,3],[1,3],[2,3]] ``` Two shortest paths of 5 moves exist: one around the top of the wall and one around the bottom. Because up is explored first, (1,3) is dequeued before (3,3), so P's parent is (1,3). The path goes around the top: (1,1) -> (0,1) -> (0,2) -> (0,3) -> (1,3) -> (2,3). ```text Input: grid = ["S#","#P"] Output: [] ``` Both neighbours of `S` are obstacles, so the search ends without reaching `P`.

Overview: Given a grid of open cells and obstacles with one start and one destination, return the list of cells on a shortest four-direction path, breaking ties with a fixed neighbour order. It tests breadth-first search, parent tracking and path reconstruction on a grid.

PracHub logo
General
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given an N x M grid as an array of strings grid. Each character is one of the following:

  • S marks the start cell (exactly one).
  • P marks the destination cell (exactly one).
  • . marks an open cell.
  • # marks an obstacle.

You may move one step up, down, left or right into any cell that is not an obstacle.

Return the shortest path from S to P as a list of [row, col] cells, starting with S's cell and ending with P's cell. If P cannot be reached, return an empty list.

Function Signature

def shortest_path(grid: list[str]) -> list[list[int]]:

Rules

  • Rows are numbered from the top and columns from the left, both starting at 0. Up means row - 1 and left means column - 1.
  • Several shortest paths can exist, so the answer must be unique: run BFS from S , exploring neighbours in the order up, down, left, right, and record a cell's parent only the first time the cell is reached. Return the path obtained by following parents back from P .
  • A cell is reached when it is first found as an in-grid, non-obstacle neighbour of the cell being expanded. That expanded cell becomes its parent and is never replaced. S has no parent.

Constraints

  • 1 <= N, M <= 100
  • grid[i].length == M
  • grid contains exactly one start cell S and exactly one destination cell P
  • Every grid[i][j] is one of the four characters S , P , . or # (start, destination, open cell or obstacle)

Examples

Input: grid = ["S..",".#.","..P"]
Output: [[0,0],[1,0],[2,0],[2,1],[2,2]]

There are two shortest paths of 4 moves: right-right-down-down and down-down-right-right. BFS checks down before right, so (1,0) is queued before (0,1). Following the parents of the cells reached first gives (0,0) -> (1,0) -> (2,0) -> (2,1) -> (2,2).

Input: grid = ["....",".S#.","..#P","...."]
Output: [[1,1],[0,1],[0,2],[0,3],[1,3],[2,3]]

Two shortest paths of 5 moves exist: one around the top of the wall and one around the bottom. Because up is explored first, (1,3) is dequeued before (3,3), so P's parent is (1,3). The path goes around the top: (1,1) -> (0,1) -> (0,2) -> (0,3) -> (1,3) -> (2,3).

Input: grid = ["S#","#P"]
Output: []

Both neighbours of S are obstacles, so the search ends without reaching P.