Problem solution · Python

Shortest Path in a Hidden Grid

Shortest Path in a Hidden Grid: 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
125 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 in a Hidden Grid, 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

  • 125 lines of Python from the credited upstream file shortest-path-in-a-hidden-grid.py.
  • The implementation visibly relies on sequence storage, hash lookup, ordered lookup.
  • No explicit loop blocks detected, together with recursive traversal.

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 in a Hidden Grid · PythonPython
Use this to learn the idea, then write your own version.
# Time:  O(m * n)# Space: O(m * n) class GridMaster(object):    def canMove(self, direction):        pass     def move(self, direction):        pass     def isTarget(self):        pass  import collections  class Solution(object):    def findShortestPath(self, master):        """        :type master: GridMaster        :rtype: int        """        directions = {'L': (0, -1), 'R': (0, 1), 'U': (-1, 0), 'D': (1, 0)}        rollback = {'L': 'R', 'R': 'L', 'U': 'D', 'D': 'U'}         def dfs(pos, target, master, lookup, adj):            if target[0] is None and master.isTarget():                target[0] = pos            lookup.add(pos)            for d, (di, dj) in directions.iteritems():                if not master.canMove(d):                    continue                nei = (pos[0]+di, pos[1]+dj)                adj[pos].add(nei)                adj[nei].add(pos)                if nei in lookup:                    continue                master.move(d)                dfs(nei, target, master, lookup, adj)                master.move(rollback[d])                                def bi_bfs(adj, start, target):            left, right = {start}, {target}            lookup = set()            steps = 0            while left:                for pos in left:                    lookup.add(pos)                new_left = set()                for pos in left:                    if pos in right:                         return steps                    for nei in adj[pos]:                        if nei in lookup:                            continue                        new_left.add(nei)                left = new_left                steps += 1                if len(left) > len(right):                     left, right = right, left            return -1                         start = (0, 0)        target = [None]        adj = collections.defaultdict(set)        dfs(start, target, master, set(), adj)        if not target[0]:            return -1        return bi_bfs(adj, start, target[0])  # Time:  O(m * n)# Space: O(m * n)class Solution2(object):    def findShortestPath(self, master):        """        :type master: GridMaster        :rtype: int        """        directions = {'L': (0, -1), 'R': (0, 1), 'U': (-1, 0), 'D': (1, 0)}        rollback = {'L': 'R', 'R': 'L', 'U': 'D', 'D': 'U'}         def dfs(pos, target, master, lookup, adj):            if target[0] is None and master.isTarget():                target[0] = pos            lookup.add(pos)            for d, (di, dj) in directions.iteritems():                if not master.canMove(d):                    continue                nei = (pos[0]+di, pos[1]+dj)                adj[pos].add(nei)                adj[nei].add(pos)                if nei in lookup:                    continue                master.move(d)                dfs(nei, target, master, lookup, adj)                master.move(rollback[d])                                def bfs(adj, start, target):            q = [start]            lookup = set(q)            steps = 0            while q:                new_q = []                for pos in q:                    if pos == target:                        return steps                    for nei in adj[pos]:                        if nei in lookup:                            continue                        lookup.add(nei)                        new_q.append(nei)                q = new_q                steps += 1            return -1                  start = (0, 0)        target = [None]        adj = collections.defaultdict(set)        dfs(start, target, master, set(), adj)        if not target[0]:            return -1        return bfs(adj, start, target[0]) 

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 ↗