Practice Shortest Path to Get All Keys
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Shortest Path to Get All Keys
Problem Statement
In grid, . is open space, # is a wall, @ is the starting cell, lowercase letters are keys, and uppercase letters are locks. Move one cell horizontally or vertically per step without crossing walls or leaving the grid. Entering a key cell collects it; a lock can be crossed only after collecting the matching lowercase key. For some k from 1 through 6, there is exactly one key and lock for each of the first k letters. Return the minimum moves needed to collect all keys, or -1 if impossible.
Source: Shortest Path to Get All Keys on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
grid = ["@.a..","###.#","b.A.B"]
8
# Example 2
grid = ["@..aA","..B#.","....b"]
6
# Example 3
grid = ["@Aa"]
-1
Constraints
Related LeetCode Problems
Practice more problems similar to Shortest Path to Get All Keys.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.