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: 3
💡 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 move to (1,2) to collect second litter in 1 move. Total: 3 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
Minimum Moves to Clean the Classroom INPUT Classroom Grid (2x3) S L R . X L Legend: S = Start L = Litter R = Reset X = Block Input Values energy = 3 grid = [["S","L","R"], [".","X","L"]] Litter count: 2 ALGORITHM (BFS) 1 Initialize BFS State: (pos, energy, litter_mask) Start: (0,0), E=3, mask=0 2 Explore Neighbors Move costs 1 energy Skip obstacles (X) 3 Handle Special Cells L: Update litter bitmask R: Reset energy to 3 4 Check Completion All litter collected? mask == (1 left-shift n) - 1 BFS Traversal Path Move 1: S-->L (0,0)-->(0,1) E:2 Move 2: L-->R (0,1)-->(0,2) E:1 [Reset energy to 3] Move 3: R-->L (0,2)-->(1,2) E:2 [All litter collected!] FINAL RESULT Optimal Path Visualization S 0 L 1 R 2 . X L 3 OUTPUT 4 Verification Total moves: 4 Litter collected: 2/2 Status: OK - Complete! Key Insight: BFS state includes position, remaining energy, AND a bitmask tracking which litter items have been collected. Reset areas (R) are crucial - they restore energy to full capacity, enabling longer paths. The bitmask ensures we find the minimum moves to collect ALL litter, not just reach a destination. TutorialsPoint - Minimum Moves to Clean the Classroom | BFS Approach
Asked in
Google 12 Amazon 8 Microsoft 6
23.4K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen