Find the fewest steps to collect every treasure in a grid maze

Quick Overview

Find the fewest steps needed to collect every treasure in a walled grid maze, starting from the top-left corner, with at most six treasures and -1 when one cannot be reached. It tests shortest paths on a grid, reasoning about the order in which the treasures are visited, and careful handling of unreachable cells.

Find the fewest steps to collect every treasure in a grid maze

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

You are given an `m x n` grid `maze` where `0` represents an open cell, `1` represents a wall, and `2` represents a treasure. You start at position `(0, 0)`, which is guaranteed to be open (value `0` or `2`). You can move up, down, left, or right by one cell per step. You cannot move through walls. Return the minimum number of steps required to collect all treasures in the maze. If it is impossible to collect all treasures, return `-1`. ### Function Signature ```python def min_steps_to_collect_treasures(maze: list[list[int]]) -> int: ``` ### Rules - `maze[r][c]` is the cell in row `r` and column `c`, both 0-indexed. Moves must stay inside the grid. - Treasure cells are open. A treasure is collected as soon as you stand on its cell, including cells you only pass through on the way somewhere else. - If `(0, 0)` holds a treasure, it is collected at step 0. - The walk ends at the last treasure collected. You do not return to the start. ### Constraints - `1 <= m, n <= 50` - `maze[i][j]` is `0`, `1`, or `2` - `maze[0][0]` is `0` or `2` - The number of treasures is at most `6` - There is at least one treasure in the maze ### Examples ```text Input: maze = [[0, 0, 0, 2], [1, 1, 0, 1], [2, 0, 0, 0], [0, 0, 1, 2]] Output: 12 ``` Walking `(0,0) -> (0,1) -> (0,2) -> (0,3)` collects the first treasure in 3 steps, going back through `(1,2)` and `(2,2)` to `(2,0)` takes 5 more, and `(2,0) -> (2,1) -> (2,2) -> (2,3) -> (3,3)` takes 4, for 12 in total. Collecting `(3,3)` before `(2,0)` also costs 12, while every order that does not start with `(0,3)` costs 15 or 16. ```text Input: maze = [[0, 0, 2], [0, 1, 0], [0, 0, 2]] Output: 4 ``` Collecting `(0,2)` first takes 2 steps, and `(0,2) -> (1,2) -> (2,2)` takes 2 more, for 4. Going to `(2,2)` first takes 4 steps around either side of the wall and then 2 more to `(0,2)`, for 6. ```text Input: maze = [[0, 1], [1, 2]] Output: -1 ``` Both neighbors of the start are walls, so the treasure at `(1,1)` cannot be reached.

Overview: Find the fewest steps needed to collect every treasure in a walled grid maze, starting from the top-left corner, with at most six treasures and -1 when one cannot be reached. It tests shortest paths on a grid, reasoning about the order in which the treasures are visited, and careful handling of unreachable cells.

PracHub logo
General
Nov 25, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

You are given an m x n grid maze where 0 represents an open cell, 1 represents a wall, and 2 represents a treasure.

You start at position (0, 0), which is guaranteed to be open (value 0 or 2). You can move up, down, left, or right by one cell per step. You cannot move through walls.

Return the minimum number of steps required to collect all treasures in the maze. If it is impossible to collect all treasures, return -1.

Function Signature

def min_steps_to_collect_treasures(maze: list[list[int]]) -> int:

Rules

  • maze[r][c] is the cell in row r and column c , both 0-indexed. Moves must stay inside the grid.
  • Treasure cells are open. A treasure is collected as soon as you stand on its cell, including cells you only pass through on the way somewhere else.
  • If (0, 0) holds a treasure, it is collected at step 0.
  • The walk ends at the last treasure collected. You do not return to the start.

Constraints

  • 1 <= m, n <= 50
  • maze[i][j] is 0 , 1 , or 2
  • maze[0][0] is 0 or 2
  • The number of treasures is at most 6
  • There is at least one treasure in the maze

Examples

Input: maze = [[0, 0, 0, 2],
               [1, 1, 0, 1],
               [2, 0, 0, 0],
               [0, 0, 1, 2]]
Output: 12

Walking (0,0) -> (0,1) -> (0,2) -> (0,3) collects the first treasure in 3 steps, going back through (1,2) and (2,2) to (2,0) takes 5 more, and (2,0) -> (2,1) -> (2,2) -> (2,3) -> (3,3) takes 4, for 12 in total. Collecting (3,3) before (2,0) also costs 12, while every order that does not start with (0,3) costs 15 or 16.

Input: maze = [[0, 0, 2],
               [0, 1, 0],
               [0, 0, 2]]
Output: 4

Collecting (0,2) first takes 2 steps, and (0,2) -> (1,2) -> (2,2) takes 2 more, for 4. Going to (2,2) first takes 4 steps around either side of the wall and then 2 more to (0,2), for 6.

Input: maze = [[0, 1],
               [1, 2]]
Output: -1

Both neighbors of the start are walls, so the treasure at (1,1) cannot be reached.