Minimum Moves to Clean the Classroom - Problem
Classroom Cleanup Challenge

You're helping a volunteer student clean up a classroom represented by an m x n grid. The student needs to collect all scattered litter while managing their limited energy.

Grid Elements:
'S' - Starting position of the student
'L' - Litter to be collected (becomes empty after collection)
'R' - Reset stations that restore energy to full capacity
'X' - Obstacles that block movement
'.' - Empty walkable spaces

Movement Rules:
• Move up, down, left, or right to adjacent cells
• Each move costs 1 energy unit
• Student starts with energy units of energy
• When energy reaches 0, student can only continue from a reset station 'R'
• Reset stations can be used multiple times

Goal: Find the minimum number of moves to collect all litter, or return -1 if impossible.

Input & Output

example_1.py — Basic classroom with litter
$ Input: classroom = [['S', '.', 'L'], ['.', 'X', '.'], ['R', '.', 'L']], energy = 3
Output: 6
💡 Note: Start at (0,0) with energy 3. Move right to (0,1) with energy 2, then down to (1,1) blocked by X. Must go (0,0)→(0,2) collect L→(1,2)→(2,2) collect L→(2,1)→(2,0) reset energy→explore to collect remaining litter. Total: 6 moves.
example_2.py — Energy management required
$ Input: classroom = [['S', '.', '.', 'R'], ['.', 'L', '.', '.'], ['.', '.', 'L', '.']], energy = 2
Output: 8
💡 Note: With only 2 energy, must strategically use reset station at (0,3). Path: S→R (restore)→collect litter optimally. The limited energy forces multiple trips to the reset station.
example_3.py — Impossible scenario
$ Input: classroom = [['S', 'X'], ['X', 'L']], energy = 5
Output: -1
💡 Note: The litter at (1,1) is completely blocked by obstacles. No path exists from S to L, making it impossible to clean all litter regardless of energy level.

Visualization

Tap to expand
🤖 Classroom Cleaning Robot Simulation🤖START🗑️LITTEROBSTACLE🔋RESET🔸 Optimal PathEnergy: 3/5Robot Behavior:• Moves cost 1 energy unit• Collects litter automatically• Must visit reset stations when energy = 0• Cannot pass through obstacles• Goal: Minimum moves to collect all litter
Understanding the Visualization
1
Initialize
Robot starts at position S with full battery
2
Explore
Robot moves in all directions, consuming energy per move
3
Collect
When robot finds litter L, it collects it automatically
4
Recharge
When battery is low, robot must find reset station R
5
Complete
Mission accomplished when all litter is collected
Key Takeaway
🎯 Key Insight: This problem combines shortest path finding with resource management (energy) and state tracking (collected items), making BFS with state compression the optimal approach.

Time & Space Complexity

Time Complexity
⏱️
O(4^(m*n))

In worst case, we explore 4 directions for each of m*n cells, leading to exponential combinations

n
2n
Linear Growth
Space Complexity
O(m*n)

Recursion stack depth limited by grid size in the worst case

n
2n
Linearithmic Space

Constraints

  • 1 ≤ m, n ≤ 10
  • 1 ≤ energy ≤ 20
  • The grid contains exactly one 'S'
  • Number of 'L' cells ≤ 10
  • Number of 'R' cells ≥ 0
  • Grid cells are only 'S', 'L', 'R', 'X', or '.'
Asked in
Google 23 Amazon 18 Meta 15 Microsoft 12
28.5K Views
Medium Frequency
~18 min Avg. Time
847 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