Find the Fewest Grid Moves Needed to Collect Every Key

Quick Overview

Find the Fewest Grid Moves Needed to Collect Every Key. In grid, @ is the single start, . is empty floor, and # is a wall.

Find the Fewest Grid Moves Needed to Collect Every Key

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

### Problem In `grid`, `@` is the single start, `.` is empty floor, and `#` is a wall. Lowercase letters are keys and their uppercase versions are locks. Move one cell north, south, east or west without crossing the boundary or a wall. Enter a lock only after obtaining its key; keys are retained. For some `1 <= k <= 6`, the grid contains exactly one key and one lock for each of the first k alphabet letters. Return the fewest moves needed to acquire every key, or `-1` when impossible. Opening every lock is not required. ```cpp class Solution { public: int shortestPathAllKeys(vector<string>& grid); }; ``` ### Examples **Example 1:** ```text Input: grid = ["@.aA"] Output: 2 Explanation: Two rightward moves acquire a; entering A is unnecessary. ``` **Example 2:** ```text Input: grid = ["@#a","A##","..."] Output: -1 Explanation: Both possible exits from the start are blocked, so the key cannot be reached. ``` ### Constraints - `m == grid.length`, `n == grid[i].length`, and `1 <= m,n <= 30`. - Cells contain English letters, `.`, `#`, or `@`, with exactly one `@`. - There are `1` to `6` unique keys, each with one matching lock, using the first k alphabet letters. ### Contract considerations Keys and locks use the contiguous letters a through the kth letter. Keys are not consumed by locks, and the starting cell counts as zero moves. ```hint Read the boundary condition Two visits to one cell can have different futures if their key sets differ. ``` ### Boundary reminders Keys and locks use the contiguous letters a through the kth letter. Keys are not consumed by locks, and the starting cell counts as zero moves.

Overview: Find the Fewest Grid Moves Needed to Collect Every Key. In grid, @ is the single start, . is empty floor, and # is a wall.

Google logo
Google
Jun 26, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Problem

In grid, @ is the single start, . is empty floor, and # is a wall. Lowercase letters are keys and their uppercase versions are locks. Move one cell north, south, east or west without crossing the boundary or a wall. Enter a lock only after obtaining its key; keys are retained. For some 1 <= k <= 6, the grid contains exactly one key and one lock for each of the first k alphabet letters. Return the fewest moves needed to acquire every key, or -1 when impossible. Opening every lock is not required.

class Solution {
public:
    int shortestPathAllKeys(vector<string>& grid);
};

Examples

Example 1:

Input: grid = ["@.aA"]
Output: 2
Explanation: Two rightward moves acquire a; entering A is unnecessary.

Example 2:

Input: grid = ["@#a","A##","..."]
Output: -1
Explanation: Both possible exits from the start are blocked, so the key cannot be reached.

Constraints

  • m == grid.length , n == grid[i].length , and 1 <= m,n <= 30 .
  • Cells contain English letters, . , # , or @ , with exactly one @ .
  • There are 1 to 6 unique keys, each with one matching lock, using the first k alphabet letters.

Contract considerations

Keys and locks use the contiguous letters a through the kth letter. Keys are not consumed by locks, and the starting cell counts as zero moves.

Boundary reminders

Keys and locks use the contiguous letters a through the kth letter. Keys are not consumed by locks, and the starting cell counts as zero moves.