Problem solution · C++

Minimum Path Cost in a Hidden Grid

Minimum Path Cost in a Hidden Grid: a C++ solution using breadth-first search. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Breadth-first search
Source
walkccc LeetCode Solutions
Length
79 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 Minimum Path Cost 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

  • 79 lines of C++ from the credited upstream file 1810.cpp.
  • The implementation visibly relies on sequence storage, work queue.
  • 3 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 walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeMinimum Path Cost in a Hidden Grid · C++C++
Use this to learn the idea, then write your own version.
/** * // This is the GridMaster's API interface. * // You should not implement it, or speculate about its implementation * class GridMaster { *  public: *   bool canMove(char direction); *   int std::move(char direction); *   boolean isTarget(); * }; */ class Solution { public:  int findShortestPath(GridMaster& master) {    constexpr int m = 100;    constexpr int startX = m;    constexpr int startY = m;    vector<int> target{m * 2, m * 2};    vector<vector<int>> grid(m * 2, vector<int>(m * 2, -1));    vector<vector<bool>> seen(m * 2, vector<bool>(m * 2));     // Build the grid information by DFS.    dfs(master, grid, startX, startY, target);     priority_queue<vector<int>, vector<vector<int>>, greater<>> minHeap;    minHeap.push({0, startX, startY});     // Find the steps by BFS.    while (!minHeap.empty()) {      const vector<int> tuple = minHeap.top();      const int cost = tuple[0];      const int i = tuple[1];      const int j = tuple[2];      minHeap.pop();      if (i == target[0] && j == target[1])        return cost;      if (seen[i][j])        continue;      seen[i][j] = true;      for (const auto& [dx, dy] : kDirs) {        const int x = i + dx;        const int y = j + dy;        if (x < 0 || x == 2 * m || y < 0 || y == 2 * m)          continue;        if (seen[x][y] || grid[x][y] == -1)          continue;        const int nextCost = cost + grid[x][y];        minHeap.push({nextCost, x, y});      }    }     return -1;  }  private:  static constexpr int kDirs[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};  static constexpr char charTable[4] = {'R', 'D', 'L', 'U'};   void dfs(GridMaster& master, vector<vector<int>>& grid, int i, int j,           vector<int>& target) {    if (master.isTarget()) {      target[0] = i;      target[1] = j;    }     for (int k = 0; k < 4; ++k) {      const int x = i + kDirs[k][0];      const int y = j + kDirs[k][1];      const char d = charTable[k];      const char undoD = charTable[(k + 2) % 4];      if (master.canMove(d) && grid[x][y] == -1) {        grid[x][y] = master.move(d);        dfs(master, grid, x, y, target);        master.move(undoD);      }    }  }}; 

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 ↗