I started with regular BFS and got maybe two minutes in before realizing the visited set was wrong.
Model the problem as a shortest path search in a state space where each state is (row, col, key_mask). Use BFS to explore all reachable states, updating the key mask when picking up keys and checking door access. Return the distance when reaching the end, or -1 if unreachable.
Pro tip: Mention that the state space is at most m*n*2^k, and that BFS is optimal because all edges have unit weight. Also, note that you can optimize by only considering keys that are actually present in the maze.
Clarify that the state must include position and collected keys. Represent keys as a bitmask (6 bits for a-f).
Since each move costs 1, BFS guarantees the shortest path. Use a queue to explore states level by level.
For each move, check boundaries, walls, and doors. If a door is encountered, verify the corresponding key is in the mask. If a key is picked up, update the mask.
Use a 3D visited array or a set to avoid revisiting the same state. Store distance in the queue or a separate array.
When the end cell is reached, return the current distance. If BFS exhausts all states without reaching the end, return -1.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.