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
- 121 lines of C++ from the credited upstream file minimum-generations-to-target-point.cpp.
- The implementation visibly relies on sequence storage.
- 11 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 {6public:7 int minGenerations(vector<vector<int>>& points, vector<int>& target) {8 const auto& encode = [](const auto& p) {9 return p[0] * 7 * 7 + p[1] * 7 + p[2];10 };11 12 vector<bool> lookup(7 * 7 * 7);13 int k = 0, total = 0;14 for (const auto& p : points) {15 if (lookup[encode(p)]) {16 continue;17 }18 if (p == target) {19 return k;20 }21 lookup[encode(p)] = true;22 }23 for (int i = 0; i < size(points); ++i) {24 if (i == total) {25 total = size(points);26 ++k;27 }28 for (int j = 0; j < i; ++j) {29 const auto& p = vector<int>{(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 continue;32 }33 if (p == target) {34 return k;35 }36 lookup[encode(p)] = true;37 points.emplace_back(p);38 }39 }40 return -1;41 }42};43 44454647class Solution2 {48public:49 int minGenerations(vector<vector<int>>& points, vector<int>& target) {50 const auto& encode = [](const auto& p) {51 return p[0] * 7 * 7 + p[1] * 7 + p[2];52 };53 54 vector<bool> lookup(7 * 7 * 7);55 for (const auto& p : points) {56 if (lookup[encode(p)]) {57 continue;58 }59 lookup[encode(p)] = true;60 }61 for (int i = 0, k = 0; i < size(points); ++k) {62 if (lookup[encode(target)]) {63 return k;64 }65 const auto& total = size(points);66 for (; i < total; ++i) {67 for (int j = 0; j < i; ++j) {68 const auto& p = vector<int>{(points[i][0] + points[j][0]) / 2, (points[i][1] + points[j][1]) / 2, (points[i][2] + points[j][2]) / 2};69 if (lookup[encode(p)]) {70 continue;71 }72 lookup[encode(p)] = true;73 points.emplace_back(p);74 }75 }76 }77 return -1;78 }79};80 81828384class Solution3 {85public:86 int minGenerations(vector<vector<int>>& points, vector<int>& target) {87 const auto& encode = [](const auto& p) {88 return p[0] * 7 * 7 + p[1] * 7 + p[2];89 };90 91 vector<vector<int>> q;92 vector<bool> lookup(7 * 7 * 7);93 for (const auto& p : points) {94 if (lookup[encode(p)]) {95 continue;96 }97 lookup[encode(p)] = true;98 q.emplace_back(p);99 }100 for (int k = 0; !empty(q); ++k) {101 if (lookup[encode(target)]) {102 return k;103 }104 vector<vector<int>> new_q;105 for (int i = size(points) - size(q); i < size(points); ++i) {106 for (int j = 0; j < i; ++j) {107 const auto& p = vector<int>{(points[i][0] + points[j][0]) / 2, (points[i][1] + points[j][1]) / 2, (points[i][2] + points[j][2]) / 2};108 if (lookup[encode(p)]) {109 continue;110 }111 lookup[encode(p)] = true;112 new_q.emplace_back(p);113 }114 }115 ranges::copy(new_q, std::back_inserter(points));116 q = move(new_q);117 }118 return -1;119 }120};121