Problem solution · Python

Shortest Path to Get All Keys

Shortest Path to Get All Keys: a Python solution using breadth-first search. Learn the idea, check the complexity, and read the full code, with credit to Kamyu LeetCode Solutions.

Technique
Breadth-first search
Source
Kamyu LeetCode Solutions
Length
71 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Breadth-first search

For Shortest Path to Get All Keys, the implementation explores reachable states in layers, which is the standard shape for unweighted shortest paths and minimum-step transitions.

  1. Model each valid configuration as a state and each legal move as an edge.
  2. Seed the queue with the starting state and mark it immediately.
  3. Expand each state once, recording distance or reachability for unseen neighbours.

Code notes

  • 71 lines of Python from the credited upstream file shortest-path-to-get-all-keys.py.
  • The implementation visibly relies on sequence storage, hash lookup, work queue.
  • No explicit loop blocks detected.

Complexity

Verify that each state and transition is processed only a bounded number of times; that determines the traversal cost.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from Kamyu LeetCode Solutions by kamyu104 and is used under the MIT licence.

Full codeShortest Path to Get All Keys · PythonPython
Use this to learn the idea, then write your own version.
# Time:  O(k*r*c + |E|log|V|) = O(k*r*c + (k*|V|)*log|V|)#                             = O(k*r*c + (k*(k*2^k))*log(k*2^k))#                             = O(k*r*c + (k*(k*2^k))*(logk + k*log2))#                             = O(k*r*c + (k*(k*2^k))*k)#                             = O(k*r*c + k^3*2^k)# Space: O(|V|) = O(k*2^k) import collectionsimport heapq  class Solution(object):    def shortestPathAllKeys(self, grid):        """        :type grid: List[str]        :rtype: int        """        directions = [(0, -1), (0, 1), (-1, 0), (1, 0)]         def bfs(grid, source, locations):            r, c = locations[source]            lookup = [[False]*(len(grid[0])) for _ in xrange(len(grid))]            lookup[r][c] = True            q = collections.deque([(r, c, 0)])            dist = {}            while q:                r, c, d = q.popleft()                if source != grid[r][c] != '.':                    dist[grid[r][c]] = d                    continue                for direction in directions:                    cr, cc = r+direction[0], c+direction[1]                    if not ((0 <= cr < len(grid)) and                            (0 <= cc < len(grid[cr]))):                        continue                    if grid[cr][cc] != '#' and not lookup[cr][cc]:                        lookup[cr][cc] = True                        q.append((cr, cc, d+1))            return dist         locations = {place: (r, c)                     for r, row in enumerate(grid)                     for c, place in enumerate(row)                     if place not in '.#'}        dists = {place: bfs(grid, place, locations) for place in locations}         # Dijkstra's algorithm        min_heap = [(0, '@', 0)]        best = collections.defaultdict(lambda: collections.defaultdict(                                                   lambda: float("inf")))        best['@'][0] = 0        target_state = 2**sum(place.islower() for place in locations)-1        while min_heap:            cur_d, place, state = heapq.heappop(min_heap)            if best[place][state] < cur_d:                continue            if state == target_state:                return cur_d            for dest, d in dists[place].iteritems():                next_state = state                if dest.islower():                    next_state |= (1 << (ord(dest)-ord('a')))                elif dest.isupper():                    if not (state & (1 << (ord(dest)-ord('A')))):                        continue                if cur_d+d < best[dest][next_state]:                    best[dest][next_state] = cur_d+d                    heapq.heappush(min_heap, (cur_d+d, dest, next_state))        return -1  

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗