Shortest Path to Get All Keys
You are given an m x n grid where each cell is one of:
@is the starting position (exactly one cell)..is an empty passable cell.#is a wall.atofare keys. Stepping on a key collects it automatically.AtoFare locks. A lock cell is passable only while you hold the matching key (aunlocksA).
In one step you may move up, down, left, or right to an adjacent cell. Return the minimum number of steps needed to collect every key on the grid, or -1 if it is impossible.
Examples
Input: ["@.a.","##b#","A.B."]
Output: 3
Input: ["@..aA","..B..","....."]
Output: 3
Hints
A cell reached with a different set of keys is a different search state. Track `(row, col, keyMask)` and run BFS on that state graph.
Map each key letter to one bit of a mask. Count the keys first so the full mask is `(1 << keyCount) - 1`.
A lock is passable only when its matching key bit is set, so the same cell can be blocked early and passable later.
Related Problems
Shortest Path to Get All Keys
You are given an `m x n` grid where each cell is one of: