Collect Every Key
grid is a map, one string per row: # is a wall, . is open floor, @ is where you start, a lowercase letter is a key and an uppercase letter is a lock.
One move goes up, down, left or right, never into a wall and never off the map. Stepping on a key picks it up. A lock can be entered only with the key of the same letter in hand, and stays open after that.
Return the fewest moves needed to pick up every key, or -1 if that cannot be done. No letter appears twice in the same case.
Example 1
Two moves right for key a, down through lock A, then along the bottom row to key b: 2 + 4 moves.
Example 2
Key a is one move left and key b then three moves right. The other order would take five.
Example 3
Key b lies behind lock A, and there is no key a anywhere.
Constraints
1 <= len(grid) <= 10and1 <= len(grid[0]) <= 10- exactly one
@; keys and locks use the letters a to f - at most 6 keys
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.