Walking a grid with keys and locks: you can revisit a cell if and only if you carry a different key inventory. The state space is $(r, c, \text{keyMask})$ with bitwise gating for locks.
(mask & (1 << (L - 'A'))) != 0.
"Because we can re-traverse empty corridors after picking up a key, the state cannot be just $(r, c)$. The state is the 3-tuple $(r, c, \text{keyMask})$."
"There are at most 6 keys ($a$ to $f$). We encode collected keys as bits in an integer: key $ch$ sets bit $(1 \ll (ch - 'a'))$. The target mask with $K$ keys is $(1 \ll K) - 1$."
"When encountering uppercase lock $L$, we check (mask & (1 << (L - 'A'))) != 0. If we hold the key, the door acts as an empty space; otherwise, it acts as a wall."
"The total number of states is $M \cdot N \cdot 2^K$. With $K \le 6$, $2^K \le 64$, giving at most $30 \times 30 \times 64 \approx 57,600$ states. Standard BFS runs in $O(MN \cdot 2^K)$ time."