Problem solution · C++

Minimum Moves to Clean the Classroom

Minimum Moves to Clean the Classroom: a C++ 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
60 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 Moves to Clean the Classroom, 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

  • 60 lines of C++ from the credited upstream file minimum-moves-to-clean-the-classroom.cpp.
  • The implementation visibly relies on sequence storage, hash lookup.
  • 5 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 codeMinimum Moves to Clean the Classroom · C++C++
Use this to learn the idea, then write your own version.
// Time:  O(m * n * 2^l)// Space: O(m * n * 2^l) // bfs, bitmasksclass Solution {public:    int minMoves(vector<string>& classroom, int energy) {        static const vector<pair<int, int>> DIRECTIONS = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};         const int m = size(classroom), n = size(classroom[0]);        unordered_map<int, int> lookup;        int r = -1, c = -1;        for (int i = 0; i < m; ++i) {            for (int j = 0; j < n; ++j) {                if (classroom[i][j] == 'S') {                    r = i;                    c = j;                } else if (classroom[i][j] == 'L') {                    lookup[i * n + j] = size(lookup);                }            }        }                vector<vector<vector<int>>> lookup2(m, vector<vector<int>>(n, vector<int>(1 << size(lookup), -1)));        lookup2[r][c][0] = energy;        vector<tuple<int, int, int, int>> q = {{r, c, 0, energy}};        for (int result = 0; !empty(q); ++result) {            vector<tuple<int, int, int, int>> new_q;            for (const auto& [i, j, mask, e] : q) {                if (lookup2[i][j][mask] != e) {                    continue;                }                if (mask == (1 << size(lookup)) - 1) {                    return result;                }                for (const auto& [di, dj] : DIRECTIONS) {                    const int ni = i + di, nj = j + dj;                    int ne = e - 1;                    if (!(0 <= ni && ni < m && 0 <= nj && nj < n && classroom[ni][nj] != 'X' && ne >= 0)) {                        continue;                    }                    int new_mask = mask;                    if (classroom[ni][nj] == 'R') {                        ne = energy;                    } else if (classroom[ni][nj] == 'L') {                        new_mask |= 1 << lookup[ni * n + nj];                    }                    if (ne <= lookup2[ni][nj][new_mask]) {                        continue;                    }                    lookup2[ni][nj][new_mask] = ne;                    new_q.emplace_back(ni, nj, new_mask, ne);                }            }            q = move(new_q);        }        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 ↗