Minimum Moves to Clean the Classroom

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, and 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 is impossible.

Example 1
Inputclassroom = ["S.","XL"], energy = 2
Output2
The student can move from (0, 0) to (0, 1) and then to (1, 1) to collect the only litter in 2 moves.
Example 2
Inputclassroom = ["LS","RL"], energy = 4
Output3
The student can collect the first litter, move to the reset area to restore energy, and then collect the second litter in 3 moves.

Constraints

  • 1 <= m == classroom.length <= 20
  • 1 <= n == classroom[i].length <= 20
  • classroom[i][j] is one of 'S', 'L', 'R', 'X', or '.'.
  • 1 <= energy <= 50
  • There is exactly one 'S' in the grid.
  • There are at most 10 'L' cells in the grid.

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate