Approach
Breadth-first search
For Minimum Generations to Target Point, the implementation explores reachable states in layers, which is the standard shape for unweighted shortest paths and minimum-step transitions.
- Model each valid configuration as a state and each legal move as an edge.
- Seed the queue with the starting state and mark it immediately.
- Expand each state once, recording distance or reachability for unseen neighbours.
Code notes
- 111 lines of Python from the credited upstream file minimum-generations-to-target-point.py.
- The implementation visibly relies on sequence storage.
- 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.
Use this to learn the idea, then write your own version.
123 45class Solution(object):6 def minGenerations(self, points, target):7 """8 :type points: List[List[int]]9 :type target: List[int]10 :rtype: int11 """12 def encode(p):13 return p[0]*7*7+p[1]*7+p[2]14 15 lookup = [False]*(7**3)16 k = total = 017 for p in points:18 if lookup[encode(p)]:19 continue20 if p == target:21 return k22 lookup[encode(p)] = True23 i = 024 while i < len(points):25 if i == total:26 total = len(points)27 k += 128 for j in xrange(i):29 p = [(points[i][0]+points[j][0])2, (points[i][1]+points[j][1])2, (points[i][2]+points[j][2])2]30 if lookup[encode(p)]:31 continue32 if p == target:33 return k34 lookup[encode(p)] = True35 points.append(p)36 i += 137 return -138 39 40414243class Solution2(object):44 def minGenerations(self, points, target):45 """46 :type points: List[List[int]]47 :type target: List[int]48 :rtype: int49 """50 def encode(p):51 return p[0]*7*7+p[1]*7+p[2]52 53 lookup = [False]*(7**3)54 for p in points:55 if lookup[encode(p)]:56 continue57 lookup[encode(p)] = True58 i = k = 059 while i < len(points):60 if lookup[encode(target)]:61 return k62 total = len(points)63 while i < total:64 for j in xrange(i):65 p = [(points[i][0]+points[j][0])2, (points[i][1]+points[j][1])2, (points[i][2]+points[j][2])2]66 if lookup[encode(p)]:67 continue68 lookup[encode(p)] = True69 points.append(p)70 i += 171 k += 172 return -173 74 75767778class Solution3(object):79 def minGenerations(self, points, target):80 """81 :type points: List[List[int]]82 :type target: List[int]83 :rtype: int84 """85 def encode(p):86 return p[0]*7*7+p[1]*7+p[2]87 88 q = []89 lookup = [False]*(7**3)90 for p in points:91 if lookup[encode(p)]:92 continue93 lookup[encode(p)] = True94 q.append(p)95 k = 096 while q:97 if lookup[encode(target)]:98 return k99 new_q = []100 for i in xrange(len(points)-len(q), len(points)):101 for j in xrange(i):102 p = [(points[i][0]+points[j][0])2, (points[i][1]+points[j][1])2, (points[i][2]+points[j][2])2]103 if lookup[encode(p)]:104 continue105 lookup[encode(p)] = True106 new_q.append(p)107 points.extend(new_q)108 q = new_q109 k += 1110 return -1111