Minimum Moves to Clean the Classroom - Problem
You are given an m x n grid classroom where a student volunteer is tasked with cleaning up litter scattered around the room.
Each cell in the grid is one of the following:
'S': Starting position of the student'L': Litter that must be collected (once collected, the cell becomes empty)'R': Reset area that restores the student's energy to full capacity, regardless of their current energy level (can be used multiple times)'X': Obstacle the student cannot pass through'.': Empty space
You are also given an integer energy, representing the student's maximum energy capacity. The student starts with this energy from the starting position 'S'.
Each move to an adjacent cell (up, down, left, or right) costs 1 unit of energy. If the energy reaches 0, the student can only continue if they are on a reset area 'R', which resets the energy to its maximum capacity energy.
Return the minimum number of moves required to collect all litter items, or -1 if it's impossible.
Input & Output
Example 1 — Basic Classroom
$
Input:
classroom = [["S","L","R"],[".","X","L"]], energy = 3
›
Output:
4
💡 Note:
Start at (0,0), collect litter at (0,1) in 1 move, go to reset at (0,2) in 1 move to restore energy, then navigate to (1,2) to collect second litter in 2 moves. Total: 4 moves.
Example 2 — No Litter
$
Input:
classroom = [["S",".","R"],[".","X","."]], energy = 2
›
Output:
0
💡 Note:
No litter to collect, so 0 moves required.
Example 3 — Impossible Case
$
Input:
classroom = [["S","X","L"]], energy = 1
›
Output:
-1
💡 Note:
Cannot reach the litter at (0,2) because obstacle at (0,1) blocks the path and we don't have enough energy to go around.
Constraints
- 1 ≤ m, n ≤ 20
- 1 ≤ energy ≤ 20
- The grid contains exactly one 'S'
- The number of 'L' cells is at most 10
Visualization
Tap to expand
Understanding the Visualization
1
Input Grid
Grid with start (S), litter (L), reset (R), obstacles (X)
2
Pathfinding
Find minimum moves to collect all litter while managing energy
3
Result
Return minimum number of moves or -1 if impossible
Key Takeaway
🎯 Key Insight: Track position, collected items bitmask, and energy as a combined state in BFS
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code