human
Solve in Python:
We are given a 2-dimensional grid. "." is an empty cell, "#" is a wall, "@" is the starting point, ("a", "b", ...) are keys, and ("A", "B", ...) are locks.
We start at the starting point, and one move consists of walking one space in one of the 4 cardinal directions.  We cannot walk outside the grid, or walk into a wall.  If we walk over a key, we pick it up.  We can't walk over a lock unless we have the corresponding key.
For some 1 <= K <= 6, there is exactly one lowercase and one uppercase letter of the first K letters of the English alphabet in the grid.  This means that there is exactly one key for each lock, and one lock for each key.
and also that the letters used to represent the keys and locks were chosen in the same order as the English alphabet.
Return the lowest number of moves to acquire all keys.  If it's impossible, return -1.
 

Example 1:
Input: ["@.a.#","###.#","b.A.B"]
Output: 8

Example 2:
Input: ["@..aA","..B#.","....b"]
Output: 6

 
Note:

1 <= grid.length <= 30
1 <= grid[0].length <= 30
grid[i][j] contains only '.', '#', '@', 'a'-'f' and 'A'-'F'
The number of keys is in [1, 6].  Each key has a different letter and opens exactly one lock.
assistant
#Here is the Python code you requested.

class Solution:
    def shortestPathAllKeys(self, grid: List[str]) -> int:
        # bfs: x,y, ownkeys -> n*m* 2^6
        
        def have_keys(status, key_id):
            return status & (2**key_id)
        
        sx, sy = -1, -1
        key_number = 0
        key_idx = 'abcdef'
        lock_idx = 'ABCDEF'
        n, m = len(grid), len(grid[0])
        for i in range(n):
            for j in range(m):
                if grid[i][j] == '@':
                    sx, sy = i, j
                elif grid[i][j] in key_idx:
                    key_number = max(key_number, key_idx.find(grid[i][j]))
        #print(sx, sy)
        #print(key_number)
        # bfs: (x, y, ownkeys)
        final_ownkey = 2 ** (key_number+1) - 1
        head, queue = 0, [(sx, sy, 0)]
        moves = [(0,1), (0,-1), (1,0), (-1,0)]
        
        visited = {(sx, sy, 0): 0}
        
        while head < len(queue):
            x, y, status = queue[head]
            step = visited[queue[head]]
            #print(x, y, status)
            #print(step)
            for dx, dy in moves:
                tx, ty, new_status = x + dx, y + dy, status
                if n > tx >= 0 and m > ty >=0:
                    # wall?
                    if grid[tx][ty] == '#':
                        continue
                        
                    # locked
                    if grid[tx][ty] in lock_idx and not have_keys(new_status, lock_idx.find(grid[tx][ty])):
                        continue
                        
                    # first time pick up key
                    if grid[tx][ty] in key_idx and not have_keys(new_status, key_idx.find(grid[tx][ty])):
                        new_status += 2 ** key_idx.find(grid[tx][ty])
                    
                    if new_status == final_ownkey:
                        return step + 1
                    
                    if (tx, ty, new_status) not in visited:
                        queue.append((tx, ty, new_status))
                        visited[(tx, ty, new_status)] = step + 1
  ...