KatInterview

Grid Maze Shortest Path with A* Search

Trees & GraphsAuto-graded · 23 test cases
MediumAuto-gradedTrees & Graphsgraph-searcha-starbfs

Last updated

Tests knowledge of informed graph search, admissible heuristics, priority-queue mechanics, and canonical path reconstruction on a 2-D grid.

Grid Maze Shortest Path with A* Search

You are given a rectangular grid of characters representing a maze. Each cell contains one of the following:

  • '.' — walkable open space
  • '#' — a wall (impassable)
  • 'S' — the starting position (also walkable)
  • 'T' — the destination (also walkable)

From any cell you may step to an adjacent cell in one of four directions — up, down, left, or right — and every such step costs exactly 1. Diagonal moves are not allowed.

Task

Find a shortest path from S to T using the A* algorithm with the Manhattan distance as the heuristic. Return the result as a dictionary with two keys:

  • "distance" — the number of steps in the shortest path (an integer ≥ 0), or -1 if no path exists.
  • "path" — the sequence of cells along that path, each cell represented as a two-element list [row, col] (0-indexed, row 0 is the top row). The list starts with S and ends with T. Return an empty list when no path exists.

Path tie-breaking rule

Multiple shortest paths of equal length may exist. Among all shortest paths, return the lexicographically smallest one. A path P is lexicographically smaller than a path Q if, at the first position where they differ, P's cell [r1, c1] is smaller than Q's cell [r2, c2] — comparing row first, then column.

Failure conditions

Return {"distance": -1, "path": []} when:

  • the grid is empty or any row has a different length from the others,
  • S or T is absent from the grid,
  • no path connects S to T (e.g., the destination is fully surrounded by walls).

Constraints

  • 1 ≤ m, n ≤ 100 where m is the number of rows and n is the number of columns.
  • Every row has the same length (the grid is rectangular).
  • There is at most one S and at most one T in the grid.
  • S and T are distinct cells.
  • Only '#' cells are impassable; 'S', 'T', and '.' are all walkable.

Examples

Example 1

Input: find_maze_path(['S..', '.#.', '..T'])

Output: {'distance': 4, 'path': [[0, 0], [0, 1], [0, 2], [1, 2], [2, 2]]}

Example 2

Input: find_maze_path(['S#.', '.#.', '..T'])

Output: {'distance': 4, 'path': [[0, 0], [1, 0], [2, 0], [2, 1], [2, 2]]}

Example 3

Input: find_maze_path(['S', 'T'])

Output: {'distance': 1, 'path': [[0, 0], [1, 0]]}

Hints

Hint 1

The Manhattan distance |Δrow| + |Δcol| to the target is an admissible heuristic for 4-directional unit-cost movement — it never overestimates the true remaining steps because every step reduces at most one of those two absolute differences by 1.

Hint 2

A* with a consistent (monotone) heuristic guarantees that the first time a cell is popped from the priority queue its distance is already optimal, so you can skip any pop whose recorded cost no longer matches the best known distance — no decrease-key operation is needed.

Hint 3

To enforce the lexicographic tie-break, run a reverse BFS from T after A* finishes to compute the step-count from every reachable cell to T, then greedily reconstruct the path from S by always picking the smallest (row, col) neighbor whose distance-to-target is exactly one less than the current cell's.